Jump to content

[SOLVED] Mysql Insert


jonoc33

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/73079-solved-mysql-insert/#findComment-368540
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/73079-solved-mysql-insert/#findComment-368547
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.