amp_rsa Posted August 29, 2007 Share Posted August 29, 2007 hi need help witha problem ive come across...i've got a form that i use on my web page which uses a mysql database....the prob is every time i reloead the page that the form is on it adds a blank record into my mysql database! Help please..what am i doing wrong! here is my code: <title>RB AH</title> </head> <body> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("rbah", $con); $sql="INSERT INTO item (username, name2, item, details, price) VALUES ('$_POST[username]','$_POST[name2]','$_POST[item]','$_POST[details]','$_POST[price]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } $result = mysql_query("SELECT * FROM item"); echo "<table border='1'> <tr> <th>username</th> <th>name2</th> <th>item</th> <th>details</th> <th>price</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>"; echo "<td>" . $row['name2'] . "</td>"; echo "<td>" . $row['item'] . "</td>"; echo "<td>" . $row['details'] . "</td>"; echo "<td>" . $row['price'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> <form action="test.php" method="post"> Username:<br/><input name="username" type="text" size="23" /><br/> Character name:<br/><input name="name2" type="text" size="23" /><br/> Item:<br/><input name="item" type="text" size="39" /><br/> Item Details:<br/><input name="details" type="text" size="39" /> <br/> Price:<br/><input name="price" type="text" size="10" /><br/><br/> <input name="submit" type="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/ Share on other sites More sharing options...
trq Posted August 29, 2007 Share Posted August 29, 2007 Wrap you entire query with an if() that checks wether or not the form has been submitted. ie; <?php if (isset($_POST['submit'])) { // query goes here. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337361 Share on other sites More sharing options...
phpSensei Posted August 29, 2007 Share Posted August 29, 2007 What is the action of your form? if its PHP_SELF, then add a <?php if(isset($_POST['button name'])){ /////Your Query } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337362 Share on other sites More sharing options...
phpSensei Posted August 29, 2007 Share Posted August 29, 2007 sorry, we posted at the same time.... Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337363 Share on other sites More sharing options...
amp_rsa Posted August 29, 2007 Author Share Posted August 29, 2007 the action is for users to enter items and have them show on same page. Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337364 Share on other sites More sharing options...
amp_rsa Posted August 29, 2007 Author Share Posted August 29, 2007 Sorry im a real noob at this can u explain a bit more on this...where should i put it...sorry! <?php if (isset($_POST['submit'])) { // query goes here. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337368 Share on other sites More sharing options...
madspof Posted August 29, 2007 Share Posted August 29, 2007 This is what you need i think but if it nothing hasnt been entered into the script the user will jsut get a blank page, so you might wish to add so kind of redirect or page telling them to fill in the from . <title>RB AH</title> </head> <body> <?php if (isset($_POST['submit'])) { $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("rbah", $con); $sql="INSERT INTO item (username, name2, item, details, price) VALUES ('$_POST[username]','$_POST[name2]','$_POST[item]','$_POST[details]','$_POST[price]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } $result = mysql_query("SELECT * FROM item"); echo "<table border='1'> <tr> <th>username</th> <th>name2</th> <th>item</th> <th>details</th> <th>price</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>"; echo "<td>" . $row['name2'] . "</td>"; echo "<td>" . $row['item'] . "</td>"; echo "<td>" . $row['details'] . "</td>"; echo "<td>" . $row['price'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); } ?> <form action="test.php" method="post"> Username:<br/><input name="username" type="text" size="23" /><br/> Character name:<br/><input name="name2" type="text" size="23" /><br/> Item:<br/><input name="item" type="text" size="39" /><br/> Item Details:<br/><input name="details" type="text" size="39" /> <br/> Price:<br/><input name="price" type="text" size="10" /><br/><br/> <input name="submit" type="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337371 Share on other sites More sharing options...
amp_rsa Posted August 29, 2007 Author Share Posted August 29, 2007 thanx it did help... but i ended up splitting it in to 3 files with that bit of code in in the php file that inserts it into mysql! I need help on something else though! this is my code: <?php if (isset($_POST['submit'])) { $con = mysql_connect("localhost","root","amped_99!"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("amp26_rbah", $con); $sql="INSERT INTO item (username, name2, item, details, price) VALUES ('$_POST[username]','$_POST[name2]','$_POST[item]','$_POST[details]','$_POST[price]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } ?> What i need is, after the details have been inserted in to mysql, iwant users to be directed back to anouther page..because at the moment it stays on this one, which is blank...if you know what i mean! help pls Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337414 Share on other sites More sharing options...
nathanmaxsonadil Posted August 29, 2007 Share Posted August 29, 2007 u can use echo '<meta http-equiv="refresh" content="2;url=http://example.com">'; or header('Location: http://www.example.com/'); Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337417 Share on other sites More sharing options...
amp_rsa Posted August 29, 2007 Author Share Posted August 29, 2007 the top 1 worked m8....thanx alot your the man! Quote Link to comment https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/#findComment-337421 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.