I have copied and modified a script off the internet. The script originally deleted selected records from a mysql table query. I have modified the script to insert the selected records into another table with the insert into statement.
I would like to know how I can insert all the selected records from the mysql array into the other table with the same id.
The logic is simlar to that of an 'orderdetails' table. I want all products ordered to have the same ordernumber so they share a common value.
How can I modify the below script to insert all values from the array with a unique number?
<?php
mysql_connect("localhost", "user", "pass")or die("cannot connect");
mysql_select_db("db")or die("cannot select DB");
$sql="SELECT * FROM category";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr><td><form name="form1" method="post">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr><td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Insert multiple rows in mysql</strong></td></tr>
<tr><td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Category ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Category</strong></td></tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr><td align="center" bgcolor="#FFFFFF"><input type="checkbox" name=check[] value="
<?php echo $rows['cat_id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['cat_id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['category']; ?></td></tr>
<?php
}
?>
<tr><td colspan="3" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td></tr>
<?php
$check=$_POST['check'];
if($_REQUEST['delete']=='Delete'){
{
$sql="INSERT INTO category1 (cat_id,category)
SELECT cat_ID, category
FROM category
WHERE cat_id='$val'";
foreach($check as $key=>$value)
{
$sql="INSERT INTO category1 (cat_id,category)
SELECT cat_ID, category
FROM category
WHERE cat_id='$value'";
$final = mysql_query($sql);
if($final) {
echo "<meta http-equiv=\"refresh\" content=\"0;URL=php.php\">";
}
}
}
}
// Check if delete button active, start this
// if successful redirect to php.php
mysql_close();
?>
</table></form></td></tr></table>
INSERT ... SELECT ...?SELECT (SELECT MAX(...) FROM ... WHERE), other columns. I'd post an answr but there's way too much code in your question, and not enough description of what you are trying to do, and I don't really want to read all that code.