jacko_162 Posted October 29, 2007 Share Posted October 29, 2007 Can someone please look at this code, its an add page, that adds 3 values in the DB; ID, name, image. When i submit this form it seems to add 2 values to the DB. for example i submit the form hoping for the next ID of 55 but it adds blank values to ID 55 and also adds an ID of 56 with the submitted information. here is my code; <title>Add A Manufacturer</title><body bgcolor="#CCCCCC"> <link rel="stylesheet" type="text/css" href="style.css" media="all"> <table width="725" border="1" cellspacing="0" cellpadding="5" align="center" bgcolor="FFFFFF" bordercolor="#999999"> <tr> <td> <table width="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><? include("includes/header.php") ?></td> </tr> </table> <?php // fixed query for insert, the above one looks like an update $sql = "INSERT INTO $table2 (make, view) VALUES ('$make', '$view')"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); ?> <form enctype="multipart/form-data" action="<? echo $PHP_SELF ?>" method="post"> <table wIDth="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><table wIDth="100%" border="0" cellspacing="1" cellpadding="3" align="center"> <tr class=table1> <td colspan="4" class=table1><div align="center"><b><font size="3" face="Verdana, Arial, Helvetica, sans-serif">ADD MANUFACTURER</font></b></div></td> </tr> <tr class=table1> <td wIDth="18%" class=table1><b>NAME: </b></td> <td width="31%" class=table1><font style="text"> <input type="text" name="make" value="" /> </font></td> <td width="21%" class=table1><b>SHOW: </b></td> <td width="30%" class=table1><font style="text"> <select name="view"> <? // Query to pull information from the Database $result = mysql_query("select * from $table8 order by id ASC"); while ($row = mysql_fetch_object($result)){ ?> <option value="<?php echo $row->view; ?>"> <?php echo $row->view; ?> </option> <?}?> </select> </font></td> </tr> <tr class=table2> <td><b>IMAGES:</b><br> <font size="1">Please select the images (jpeg only):</font></td> <td colspan="3"> 1: Upload <input name="imageOne" type="file" class="bginput" id="imageOne"><br> </td> </tr> <!-- end of new code --> <tr class=table1> <td colspan="4" align="center" valign="top"> <input type="submit" name="submit" value="SUBMIT" > <input type="hidden" name="ID" value="<?php echo $ID; ?>"> <input type="reset" name="reset" value="Reset"> </td> </tr> </table> </td> </tr> </table> </FORM> <br><table width="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><? include("includes/footer.php") ?></td> </tr> </table> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/75192-solved-posts-twice/ Share on other sites More sharing options...
MadTechie Posted October 29, 2007 Share Posted October 29, 2007 I can't see why its adding two, but the reason they ar e blank is because your not setting the values see comment "//Set Post Values ADD 2 lines below" <title>Add A Manufacturer</title><body bgcolor="#CCCCCC"> <link rel="stylesheet" type="text/css" href="style.css" media="all"> <table width="725" border="1" cellspacing="0" cellpadding="5" align="center" bgcolor="FFFFFF" bordercolor="#999999"> <tr> <td> <table width="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><? include("includes/header.php") ?></td> </tr> </table> <?php //Set Post Values ADD 2 lines below $make = $_POST['make']; $view = $_POST['view']; // fixed query for insert, the above one looks like an update $sql = "INSERT INTO $table2 (make, view) VALUES ('$make', '$view')"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); ?> <form enctype="multipart/form-data" action="<? echo $PHP_SELF ?>" method="post"> <table wIDth="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><table wIDth="100%" border="0" cellspacing="1" cellpadding="3" align="center"> <tr class=table1> <td colspan="4" class=table1><div align="center"><b><font size="3" face="Verdana, Arial, Helvetica, sans-serif">ADD MANUFACTURER</font></b></div></td> </tr> <tr class=table1> <td wIDth="18%" class=table1><b>NAME: </b></td> <td width="31%" class=table1><font style="text"> <input type="text" name="make" value="" /> </font></td> <td width="21%" class=table1><b>SHOW: </b></td> <td width="30%" class=table1><font style="text"> <select name="view"> <? // Query to pull information from the Database $result = mysql_query("select * from $table8 order by id ASC"); while ($row = mysql_fetch_object($result)){ ?> <option value="<?php echo $row->view; ?>"> <?php echo $row->view; ?> </option> <?}?> </select> </font></td> </tr> <tr class=table2> <td><b>IMAGES:</b><br> <font size="1">Please select the images (jpeg only):</font></td> <td colspan="3"> 1: Upload <input name="imageOne" type="file" class="bginput" id="imageOne"><br> </td> </tr> <!-- end of new code --> <tr class=table1> <td colspan="4" align="center" valign="top"> <input type="submit" name="submit" value="SUBMIT" > <input type="hidden" name="ID" value="<?php echo $ID; ?>"> <input type="reset" name="reset" value="Reset"> </td> </tr> </table> </td> </tr> </table> </FORM> <br><table width="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><? include("includes/footer.php") ?></td> </tr> </table> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/75192-solved-posts-twice/#findComment-380271 Share on other sites More sharing options...
jacko_162 Posted October 29, 2007 Author Share Posted October 29, 2007 thank you very much MadTechie, its now posting values but it still adds 2 entries to database, 1 entry is with all the values and other is blank apart from a new ID?! Quote Link to comment https://forums.phpfreaks.com/topic/75192-solved-posts-twice/#findComment-380293 Share on other sites More sharing options...
MadTechie Posted October 29, 2007 Share Posted October 29, 2007 Ahh i see change <?php //Set Post Values ADD 2 lines below $make = $_POST['make']; $view = $_POST['view']; // fixed query for insert, the above one looks like an update $sql = "INSERT INTO $table2 (make, view) VALUES ('$make', '$view')"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); ?> to <?php if(isset($_POST['submit'])) { $make = $_POST['make']; $view = $_POST['view']; // fixed query for insert, the above one looks like an update $sql = "INSERT INTO $table2 (make, view) VALUES ('$make', '$view')"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75192-solved-posts-twice/#findComment-380295 Share on other sites More sharing options...
jacko_162 Posted October 29, 2007 Author Share Posted October 29, 2007 i tried that now i just get the error message "Unable to get insert id for image name." this is full code what i have now, <title>Add A Manufacturer</title><body bgcolor="#CCCCCC"> <link rel="stylesheet" type="text/css" href="style.css" media="all"> <table width="725" border="1" cellspacing="0" cellpadding="5" align="center" bgcolor="FFFFFF" bordercolor="#999999"> <tr> <td> <table width="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><? include("includes/header.php") ?></td> </tr> </table> <?php //Set Post Values ADD 2 lines below $make = $_POST['make']; $view = $_POST['view']; // Query for insert $sql = "INSERT INTO $table2 (make, view) VALUES ('$make', '$view')"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); // sorts out the uploaded images $upload_dir = "../images/man/"; $insert_id = mysql_insert_id() or die("Unable to get insert id for image name.<br>" . mysql_error()); if (!empty($imageOne)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageOne']['type'] != "image/jpeg" && $_FILES['imageOne']['type'] != "image/pjpeg") { die("Image type is not allowed for Image One. Type found : " . $_FILES['imageOne']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "one.jpg"; if (!copy($_FILES['imageOne']['tmp_name'], $upload_dir . $new_image_name)) { // file uploaded add to table row die("Unable to upload Image One"); } } } // end of code ?> <form enctype="multipart/form-data" action="<? echo $PHP_SELF ?>" method="post"> <table wIDth="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><table wIDth="100%" border="0" cellspacing="1" cellpadding="3" align="center"> <tr class=table1> <td colspan="4" class=table1><div align="center"><b><font size="3" face="Verdana, Arial, Helvetica, sans-serif">ADD MANUFACTURER</font></b></div></td> </tr> <tr class=table1> <td wIDth="18%" class=table1><b>NAME: </b></td> <td width="31%" class=table1><font style="text"> <input type="text" name="make" value="" /> </font></td> <td width="21%" class=table1><b>SHOW: </b></td> <td width="30%" class=table1><font style="text"> <select name="view"> <? // Query to pull information from the Database $result = mysql_query("select * from $table8 order by id ASC"); while ($row = mysql_fetch_object($result)){ ?> <option value="<?php echo $row->view; ?>"> <?php echo $row->view; ?> </option> <?}?> </select> </font></td> </tr> <tr class=table2> <td><b>IMAGES:</b><br> <font size="1">Please select the images (jpeg only):</font></td> <td colspan="3"> 1: Upload <input name="imageOne" type="file" class="bginput" id="imageOne"><br> </td> </tr> <!-- end of new code --> <tr class=table1> <td colspan="4" align="center" valign="top"> <input type="submit" name="submit" value="SUBMIT" > <input type="reset" name="reset" value="Reset"> </td> </tr> </table> </td> </tr> </table> </FORM> <br><table width="700" border="0" align="center" cellpadding="0" cellspacing="0"> <tr class=table3> <td><? include("includes/footer.php") ?></td> </tr> </table> </td> </tr> </table> it seems to add the one value fine, but also adds another one full of blank values with a different ID number (auto_incremement) anything to do with my mysql_insert_id() ??? the script renames the image files with the $id so i used mysql_insert_id() to fetch previous id number. Quote Link to comment https://forums.phpfreaks.com/topic/75192-solved-posts-twice/#findComment-380296 Share on other sites More sharing options...
MadTechie Posted October 29, 2007 Share Posted October 29, 2007 Yep.. see update (3 lines).. <?php if(isset($_POST['submit']))//ADD { //ADD $make = $_POST['make']; $view = $_POST['view']; // Query for insert $sql = "INSERT INTO $table2 (make, view) VALUES ('$make', '$view')"; $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); // sorts out the uploaded images $upload_dir = "../images/man/"; $insert_id = mysql_insert_id() or die("Unable to get insert id for image name.<br>" . mysql_error()); if (!empty($imageOne)) { // we have an image in image one check we have a valid type (jpeg only ?) if ($_FILES['imageOne']['type'] != "image/jpeg" && $_FILES['imageOne']['type'] != "image/pjpeg") { die("Image type is not allowed for Image One. Type found : " . $_FILES['imageOne']['type']); } else { // valid image make up a new filename $new_image_name = $insert_id . "one.jpg"; if (!copy($_FILES['imageOne']['tmp_name'], $upload_dir . $new_image_name)) { // file uploaded add to table row die("Unable to upload Image One"); } } } // end of code } //ADD ?> Quote Link to comment https://forums.phpfreaks.com/topic/75192-solved-posts-twice/#findComment-380299 Share on other sites More sharing options...
jacko_162 Posted October 29, 2007 Author Share Posted October 29, 2007 cheers, works a treat dude, and you're from UK also i cant thank you enough, the headaches its been causing me Quote Link to comment https://forums.phpfreaks.com/topic/75192-solved-posts-twice/#findComment-380308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.