Note that there are some explanatory texts on larger screens.

plurals
  1. POinsert multiple rows from array into table with unique ID
    text
    copied!<p>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.</p> <p>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.</p> <p>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.</p> <p>How can I modify the below script to insert all values from the array with a unique number?</p> <pre><code>&lt;?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); ?&gt; &lt;table width="400" border="0" cellspacing="1" cellpadding="0"&gt; &lt;tr&gt;&lt;td&gt;&lt;form name="form1" method="post"&gt; &lt;table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"&gt; &lt;tr&gt;&lt;td bgcolor="#FFFFFF"&gt;&amp;nbsp;&lt;/td&gt; &lt;td colspan="4" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Insert multiple rows in mysql&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td align="center" bgcolor="#FFFFFF"&gt;#&lt;/td&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Category ID&lt;/strong&gt;&lt;/td&gt; &lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;strong&gt;Category&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;?php while($rows=mysql_fetch_array($result)){ ?&gt; &lt;tr&gt;&lt;td align="center" bgcolor="#FFFFFF"&gt;&lt;input type="checkbox" name=check[] value=" &lt;?php echo $rows['cat_id']; ?&gt;"&gt;&lt;/td&gt; &lt;td bgcolor="#FFFFFF"&gt;&lt;?php echo $rows['cat_id']; ?&gt;&lt;/td&gt; &lt;td bgcolor="#FFFFFF"&gt;&lt;?php echo $rows['category']; ?&gt;&lt;/td&gt;&lt;/tr&gt; &lt;?php } ?&gt; &lt;tr&gt;&lt;td colspan="3" align="center" bgcolor="#FFFFFF"&gt;&lt;input name="delete" type="submit" id="delete" value="Delete"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;?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=&gt;$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 "&lt;meta http-equiv=\"refresh\" content=\"0;URL=php.php\"&gt;"; } } } } // Check if delete button active, start this // if successful redirect to php.php mysql_close(); ?&gt; &lt;/table&gt;&lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload