php mysql checkbox and textboxes values insert into database -
i have db table called 'ice_flavours' columns: 'id', 'flavours', 'date', 'selected'. , table called 'ice_today' like: 'id', 'flavours', 'date', 'selected'.
now, on left of page want call 'ice_flavours' , print entries in form checkbox. on right of page shall showcase want todays selected flavours shown , shall stay in there until end of today´s date , automatically discarded. when selected show in showcase, want them not show on left side.
so, when select checkbox next flavour, values 'id', 'flavours', 'date', 'selected' ought entered 'ice_today' reason last row of ice_flavours table entered , message "duplicate entry '0' key 'primary'"
maybe can done using single db table havent figured yet. can please
edit.php:
<?php ob_start(); include_once("dbinfo.inc.php"); include_once("config.php"); if(isset($_post['submit'])){ $selected = $_post['selected']; $id = $_post['id']; $flavour = $_post['flavour']; $date = $_post['date']; if($_post["submit"] == "submit") { for($i=0;$i<sizeof($selected);$i++) { if(!empty($flavour)) { echo""; echo "<pre>"; print_r($_post); echo "</pre>"; $query="insert ice_today values('$id','$flavour','$date','$selected[$i]')"; mysql_query($query) or die(mysql_error()); } } echo "entry added"; } } ?> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>edit</title> </head> <body> <form name="form1" id="form1" method="post" action="edit.php"> <table border="1" width="200px"> <td> <table border="1" width="200px"> <?php $result = mysql_query("select * ice_flavours ('selected' != '1') order flavour asc"); $results = mysql_num_rows($result); if ($results > 0) { $num=mysql_numrows($result); $i=0; while ($i < $num) { $id=mysql_result($result,$i,"id"); $flavour=mysql_result($result,$i,"flavour"); ?> <tr> <td align="center" valign="middle"> <?php echo $flavour; ?> <input type="text" name="id" id="id" value="<?php echo $id; ?>"> <input type="text" name="flavour" id="flavour" value="<?php echo $flavour; ?>"> <input type="text" name="datum" id="datum" value="<?php echo date('y-m-d'); ?>"> <input type="checkbox" name="selected[]" id="selected" value="<?php echo '1'; ?>"> </td></tr> <?php $i++; } ?> <input type="submit" value="submit"> <?php } ?> </td> <td> <table border="1" width="200px"> <tr><td align="center" valign="middle"> showcase </td></tr> <?php include_once("dbinfo.inc.php"); include_once("config.php"); $result2 = mysql_query("select * ice_today order flavour asc"); $results2 = mysql_num_rows($result2); if ($results2 > 0) { $num2=mysql_numrows($result2); $j=0; while ($j < $num2) { $id=mysql_result($result2,$j,"id"); $flavour=mysql_result($result2,$j,"flavour"); ?> <tr><td align="center" valign="middle"> <?php echo $flavour; ?> </td></tr> <?php $j++; } } echo '</td></tr></table>'; ?> </body> </html> <?php ob_end_flush(); ?>
please take @ mysql reference manual. need specify each column in insert
statement:
$query="insert ice_today(id, flavours, date, selected) values('$id','$flavour','$date','$selected[$i]')";
i suggest not use original mysql-extension anymore, deprecated of php 5.5.x. when using mysql extension (mysqli or pdo instance: mysqli or pdo - pros , cons?) can use prepared statements become safe against sql injections (which aren't right now).
post-values can manipulated , feed them query via string concatenation, you're not safe against them.
Comments
Post a Comment