Page 1 of 1

insert html form data to mysql DB

Posted: Mon Jan 17, 2011 6:10 am
by tsilis3
php newbie here. I am trying to insert a data from an html form to a mysql database. The form was generated by a wysiwyg editor and is shown below:

Code: Select all

<form name="Form2" method="post" action="addsuppliers.php" enctype="text/plain" id="Form2" onsubmit="window.open('http://localhost/xampp/addsuppliers.php);return false;">
<div id="wb_Text7" style="margin:0;padding:0;position:absolute;left:10px;top:20px;width:45px;height:14px;text-align:left;z-index:14;">
<font style="font-size:11px" color="#000000" face="Arial">name</font></div>
<textarea name="name" id="TextArea1" style="position:absolute;left:65px;top:20px;width:198px;height:23px;border:1px #C0C0C0 solid;font-family:Courier New;font-size:13px;z-index:15" rows="0" cols="20"></textarea>
<textarea name="id" id="TextArea2" style="position:absolute;left:64px;top:56px;width:198px;height:23px;border:1px #C0C0C0 solid;font-family:Courier New;font-size:13px;z-index:16" rows="0" cols="20"></textarea>
<textarea name="phone" id="TextArea3" style="position:absolute;left:64px;top:95px;width:198px;height:23px;border:1px #C0C0C0 solid;font-family:Courier New;font-size:13px;z-index:17" rows="0" cols="20"></textarea>
<textarea name="fax" id="TextArea4" style="position:absolute;left:64px;top:135px;width:198px;height:23px;border:1px #C0C0C0 solid;font-family:Courier New;font-size:13px;z-index:18" rows="0" cols="20"></textarea>
<textarea name="address" id="TextArea5" style="position:absolute;left:64px;top:174px;width:198px;height:23px;border:1px #C0C0C0 solid;font-family:Courier New;font-size:13px;z-index:19" rows="0" cols="20"></textarea>
<textarea name="afm" id="TextArea6" style="position:absolute;left:64px;top:214px;width:198px;height:23px;border:1px #C0C0C0 solid;font-family:Courier New;font-size:13px;z-index:20" rows="0" cols="20"></textarea>
<div id="wb_Text8" style="margin:0;padding:0;position:absolute;left:10px;top:66px;width:45px;height:14px;text-align:left;z-index:21;">
<font style="font-size:11px" color="#000000" face="Arial">id</font></div>
<div id="wb_Text9" style="margin:0;padding:0;position:absolute;left:11px;top:100px;width:45px;height:14px;text-align:left;z-index:22;">
<font style="font-size:11px" color="#000000" face="Arial">phone</font></div>
<div id="wb_Text10" style="margin:0;padding:0;position:absolute;left:12px;top:142px;width:45px;height:14px;text-align:left;z-index:23;">
<font style="font-size:11px" color="#000000" face="Arial">fax</font></div>
<div id="wb_Text11" style="margin:0;padding:0;position:absolute;left:9px;top:180px;width:52px;height:14px;text-align:left;z-index:24;">
<font style="font-size:11px" color="#000000" face="Arial">address</font></div>
<div id="wb_Text12" style="margin:0;padding:0;position:absolute;left:14px;top:221px;width:45px;height:14px;text-align:left;z-index:25;">
<font style="font-size:11px" color="#000000" face="Arial">afm</font></div>
<input type="submit" id="Button2" name="" value="Submit" style="position:absolute;left:108px;top:258px;width:96px;height:25px;font-family:Arial;font-size:13px;z-index:26">
</form>

the best thing i got so far is a new browser window showing me my php script which is the following:

Code: Select all

<?php
$con = mysql_connect("localhost","root","panionios");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("diogenis", $con);

$sql="INSERT INTO suppliers (ID, name, afm, phone, address, fax)
VALUES
('$_POST[id]','$_POST[name]','$_POST[afm]','$_POST[phone]','$_POST[address]','$_POST[fax]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
I am using xampp and it shows that php is working fine but the database won't update.
What am i doing wrong??

Re: insert html form data to mysql DB

Posted: Mon Jan 17, 2011 10:38 am
by social_experiment
Include an else statement in your code and see what the result is. The insert query looks fine you should just use mysql_real_escape_string() to escape all values received from user input.

Code: Select all

<?php
$con = mysql_connect("localhost","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("diogenis", $con);

$sql="INSERT INTO suppliers (ID, name, afm, phone, address, fax)
VALUES
('$_POST[id]','$_POST[name]','$_POST[afm]','$_POST[phone]','$_POST[address]','$_POST[fax]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
// add else statement
else {
 echo "1 record added";
}
mysql_close($con)
?>