jonoc33 Posted October 13, 2007 Share Posted October 13, 2007 How exactly would I do a Mysql Insert query from a form? Quote Link to comment https://forums.phpfreaks.com/topic/73079-solved-mysql-insert/ Share on other sites More sharing options...
kenrbnsn Posted October 13, 2007 Share Posted October 13, 2007 It depends on your form and your MySql table definition. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73079-solved-mysql-insert/#findComment-368539 Share on other sites More sharing options...
jonoc33 Posted October 13, 2007 Author Share Posted October 13, 2007 scrimform.php <form method="POST" action="scrimsubmit.php"> <label><strong>Submit a Scrim</strong><br /> Server Name:<br /> <input name="servername" type="text" id="servername" size="40" /> </label> <br /> <label>IP Address:<br /> <input type="text" name="ip" id="ip" /> </label> <br /> <label>Time:<br /> <input name="time" type="text" id="time" size="10" /> </label> <br /> <label>Date:<br /> <input name="date" type="text" id="date" size="15" /> </label> <br /> <label>Password:<br /> <input name="password" type="text" id="password" size="20" /> </label> <br /> <input type="submit" name="submit" value="Submit" /> </form> This is the code used for the form. When hitting submit it takes you to scrimsubmit.php. scrimsubmit.php <?php $con = mysql_connect("localhost","*******","******"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("********", $con);$sql="INSERT INTO ******* (servername, ipaddress, time, date, password) VALUES ('"$_POST["servername"]"','"$_POST["ipaddress"]"','"$_POST["time"]"','"$_POST["date"]"','"$_POST["password"]")"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> This is what happens. Nothing happens, unfortunately. Quote Link to comment https://forums.phpfreaks.com/topic/73079-solved-mysql-insert/#findComment-368540 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2007 Share Posted October 13, 2007 You have errors in your code and the layout is horrible. Try this: <?php $con = mysql_connect("localhost","*******","******"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("********", $con); $sql="INSERT INTO ******* (servername, ipaddress, time, date, password) VALUES ('" . $_POST["servername"] . "','" . $_POST["ipaddress"] . "','" . $_POST["time"] . "','" . $_POST["date"] . "','" . $_POST["password"] . "')"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br>" . mysql_error); echo "1 record added"; ?> BTW, it is very bad to insert values directly from the form into the database without validating them. At least pass all fields through the function mysql_real_escape_string as the follow illustrates: <?php $con = mysql_connect("localhost","*******","******"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("********", $con); $qtmp = array(); foreach (array('servername','ipaddress','time','date') as $key) $qtmp = $key . " = '" . mysql_real_escape_string($_POST[$key] . "'"; $sql = "insert into ***** set " . implode(', ',$qtmp); $rs = mysql_query($sql) or die ("Problem with the query: $sql <br>" . mysql_error); echo "1 record added"; ?> The above code makes use of the alternative form of the "insert" command. Ken Quote Link to comment https://forums.phpfreaks.com/topic/73079-solved-mysql-insert/#findComment-368547 Share on other sites More sharing options...
jonoc33 Posted October 13, 2007 Author Share Posted October 13, 2007 Thanks, that worked Quote Link to comment https://forums.phpfreaks.com/topic/73079-solved-mysql-insert/#findComment-368551 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.