vinataba vinataba - 2 months ago 11
MySQL Question

php - Can not insert multiple text inputs into database

This is my html code.

<form class="form-submit" method="post" action="sign-up-form.php" >
<div id="change-color0">
<label><span class="turn-white0">01</span>Họ tên đầy đủ của bạn</label>
<input type="text" id="input" name="content[]" class="addtodo0">
</div>
<div id="change-color1">
<label><span class="turn-white1">02</span>Số chứng minh thư nhân dân của bạn</label>
<input type="text" id="input" name="content[]" class="addtodo1">
</div>
<div id="change-color2">
<label><span class="turn-white2">03</span>Địa chỉ thường trú của bạn</label>
<input type="text" id="input" name="content[]" class="addtodo2">
</div>
<div id="change-color3">
<label><span class="turn-white3">04</span>Tại sao bạn muốn trở thành học viên của dự án</label>
<input type="text" id="input" name="content[]" class="addtodo3">
</div>
<div id="change-color4">
<label><span class="turn-white4">05</span>Bạn nghĩ mình là ai</label>
<input type="text" id="input" name="content[]" class="addtodo4">
</div>
<div id="change-color5">
<label><span class="turn-white5">06</span>Trong mắt người khác bạn là ai</label>
<input type="text" id="input" name="content[]" class="addtodo5">
</div>
<div id="change-color6">
<label><span class="turn-white6">07</span>Bạn sợ nhất điều gì</label>
<input type="text" id="input" name="content[]" class="addtodo6">
</div>
<div id="change-color7">

<div class="row" id="log-out">
<div id="submit-button">
<button class="btn btn-sharp">
<a name="submit" value="submit" id="submit-form">Gửi đơn</a>
</button>
</div>

<div id="log-out-button">
<a href="homepage.html" id="log-out">ĐĂNG XUẤT</a>
</div>
</div>

</form>


And this is my php code to insert the text fields into my database

<?php

if (isset($_POST["submit"])){

$conn = mysql_connect("localhost", "root","");
mysql_select_db("db");

foreach ($_POST['content'] as $content) {
$data = mysql_real_escape_string($content);
mysql_query("INSERT INTO form ( content ) VALUES ('".$data."')") or die(mysql_error());
}
}
header("location: thank-you.html");
?>


Text editor doesnt show any error but the database shows nothing. I've tried


implode()


UPDATE (php code)
I updated my code using PDO like this.

<?php

if (isset($_POST["submit"])){

$pdo = new PDO("mysql: host = localhost; dbname = db","root","");

die(var_dump($_POST['content']));

foreach ($_POST['content'] as $content) {
$query = $pdo->prepare("INSERT INTO form(content) VALUES (:content)");
$query->bindParam(":content", $content);
$query->execute();
}
}

?>


And thing does not show up still.

Answer

You should try this. I read this on stackover and it works.

if (isset($_POST["submit"]))
    foreach ($_POST['content'] as $content) {
        $data1 = mysql_real_escape_string($content);
        mysql_query("INSERT INTO form (content) VALUES ('$data1')") or die(mysql_error());
    }
}
Comments