Jump to content

MySQL insert multiple records from select please view code


Recommended Posts

Hi I want to bring this information to insert multiple records from select

<?PHP
    error_reporting(0);
    include("connect.inc.php");     
?>


<?php
include "connect.inc.php";

if (isset($_POST['submit'])) {
$province_id = $_POST['province_id'][$i];
$province_id = $_POST['travel_id'][$i];

$i = 0;
foreach ( $_POST as $val) {


mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ('$province_id', '$travel_id')");
$i++;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="js/bootstrap.min.js"></script>
<title>Untitled Document</title>
</head>

<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="respond.js"></script>
<form action="index.php" method="post"> <!--ส่งค่า post ไปหน้าเดิม -->
<table width="896" border="1">
    <thead>
    <tr>
    <th width="239">จังหวัด</th>
<th width="552">สถานที่ท่องเที่ยว</th>
<th width="83"><input type="hidden" id="txtNum" value="1" size="2" /></th>
</tr>
<tr>
    <th width="239">
    <select id="selProvince" name="province_id[]"> <!--Default จังหวััด-->
        <option value="">กรุณาเลือกจังหวัด</option>
                <?PHP
                    $SelectPr="SELECT * FROM province";
                    $QueryPro=mysql_query($SelectPr);
                    while($Pro=mysql_fetch_array($QueryPro)){
                ?>
    <option value="<?=$Pro['province_id']?>"><?=$Pro['province_name']?></option>
<?PHP } ?>
</select>
</th>
<th width="552"><select name ="travel_id[]" id="selTravel"><option value="">กรุณาเลือกจังหวัด</option></select></th>
<th width="83"><button type="button" id="btnP">เพิ่มรายการ</button></th>
</tr>
<tr><td colspan="3"><center>รายการที่เพิ่ม</center></td></tr>
</thead>

<tbody>

</tbody>

</table>
<input name="submit" type="submit" value="add">

</form>
<br><br><br>
<!--ทอสอบ ค่าแสดงผล Muti Atrray-->



</body>
</html>

I'm insert 1 record but I choose just one , but more data is imported into fifth data .

 

20160624220820.png?v=1001

Informationprovince_id, travel_id not insert into

20160624221335.png?v=1001

 

HELP ME HOT FIX CODE

 

 

I choose just one , but more data is imported into fifth data .

Link to post
Share on other sites

error_reporting(0);
That's not a good idea. If errors happen you should fix them, not hide them.

 

include("connect.inc.php");
You include that file twice, it only needs to be included once.

 

if (isset($_POST['submit'])) {
$province_id = $_POST['province_id'][$i];
$province_id = $_POST['travel_id'][$i];

$i = 0;
foreach ( $_POST as $val) {


mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ('$province_id', '$travel_id')");
$i++;
}
}
You're trying to use a variable called $travel_id in the query, but you don't have that variable. You saved both values as $province_id. You also don't want to loop over all of $_POST, if you have an array of values then you loop over the array inside $_POST. But your form doesn't have multiple values for each of those, you only have 2 select elements and both of them only let you select 1 value each. So you don't need to loop at all, you can just get the selected values and insert them.

 

Also, your code isn't going to work in the current version of PHP, because functions like mysql_query were removed. Instead, you should use the mysqli extension, or PDO. Both of those support prepared statements, which you should also use.

Link to post
Share on other sites
error_reporting(0);
That's not a good idea. If errors happen you should fix them, not hide them.

 

include("connect.inc.php");
You include that file twice, it only needs to be included once.

 

if (isset($_POST['submit'])) {
$province_id = $_POST['province_id'][$i];
$province_id = $_POST['travel_id'][$i];

$i = 0;
foreach ( $_POST as $val) {


mysql_query("INSERT INTO travel_list (province_id, travel_id) VALUES ('$province_id', '$travel_id')");
$i++;
}
}
You're trying to use a variable called $travel_id in the query, but you don't have that variable. You saved both values as $province_id. You also don't want to loop over all of $_POST, if you have an array of values then you loop over the array inside $_POST. But your form doesn't have multiple values for each of those, you only have 2 select elements and both of them only let you select 1 value each. So you don't need to loop at all, you can just get the selected values and insert them.

 

Also, your code isn't going to work in the current version of PHP, because functions like mysql_query were removed. Instead, you should use the mysqli extension, or PDO. Both of those support prepared statements, which you should also use.

 

 

Please help me I solved this does not come for another three days.

Please help me Project BA

Time remaining 1 day

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...