Jump to content

insert into problem.


herghost

Recommended Posts

Excuse me, its been some time since I used php/mysql so I am a little rusty, I have the following which is simply meant to add some info to a database from a table:

<?php include('/config/connect.php');

$title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
$fname = (isset($_POST['fname'])) ? trim($_POST['fname']) : '';
$sname = (isset($_POST['sname'])) ? trim($_POST['sname']) : '';
$nn = (isset($_POST['nn'])) ? trim($_POST['nn']) : '';
$street1 = (isset($_POST['street1'])) ? trim($_POST['street1']) : '';
$street2 = (isset($_POST['street2'])) ? trim($_POST['street2']) : '';
$city = (isset($_POST['city'])) ? trim($_POST['city']) : '';
$state = (isset($_POST['state'])) ? trim($_POST['state']) : '';
$zip = (isset($_POST['zip'])) ? trim($_POST['zip']) : '';
$tel = (isset($_POST['tel'])) ? trim($_POST['tel']) : '';
$email = (isset($_POST['email'])) ? trim($_POST['email']) : '';
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? md5($_POST['password']): '';
$news = (isset($_POST['news'])) ? trim($_POST['news']) : '';


$query = "INSERT INTO customers 

(title, fname, sname, nn, street1, street2, city, state, zip, tel, email, username, password, news)

VALUES

('$title', '$fname', $sname', '$nn', '$street1', '$street2', '$city', '$state', '$zip', '$tel', '$email', '$username', '$password', '$news')";


if (!mysql_query($query))
  {
  die('Error: ' . mysql_error());
  }
echo "CUSTOMER ADDED";


?>




<style type="text/css">
<!--
body,td,th {
color: #000;
}
body {
background-color: #FFF;
text-align: center;
}
-->
</style>

<form action="addcustomer.php" method="post" name="addcustomer">
<table width="80%" border="0" align="center" cellpadding="1">
  
  <tr>
    <td bgcolor="#CC99FF">Title</td>
    <td bgcolor="#CC99FF"><select name="title">
    <option value="Mr">Mr</option>
    <option value="Miss">Miss</option>
    <option value="Ms">Ms</option>
    <option value="Mrs">Mrs</option>
    <option value="Dr">Dr</option>
    <option value="Prof">Prof</option>
    </select></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Customer Forename</td>
    <td bgcolor="#CC99FF"><input name="fname" type="text" size="32"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Customer Surname </td>
    <td bgcolor="#CC99FF"><input name="sname" type="text" size="32"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">House Name/Number</td>
    <td bgcolor="#CC99FF"><input name="nn" type="text" size="32"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Street Line 1</td>
    <td bgcolor="#CC99FF"><input name="street1" type="text" size="60"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Street Line 2</td>
    <td bgcolor="#CC99FF"><input name="street2" type="text" size="60"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">City</td>
    <td bgcolor="#CC99FF"><input name="city" type="text" size="32"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">State</td>
    <td bgcolor="#CC99FF"><input name="state" type="text" size="32"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Zip</td>
    <td bgcolor="#CC99FF"><input name="sname" type="text" size="10"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Phone</td>
    <td bgcolor="#CC99FF"><input name="tel" type="text" size="20"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Email</td>
    <td bgcolor="#CC99FF"><input name="email" type="text" size="36"></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Username</td>
    <td bgcolor="#CC99FF"><input name="username" type="text" size="36"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Password</td>
    <td bgcolor="#CC99FF"><input name="password" type="password" size="32"></td>
  </tr>
  <tr>
    <td bgcolor="#CC99FF">Newsletter</td>
    <td bgcolor="#CC99FF"><select name="news">
    <option value="Y">Yes</option>
    <option value="N">No</option>
    </select></td>
  </tr>
</table>
<input name="submit" type="submit" value="Add Customer">
</form>

 

However I am recieving this error:

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', '', '', '', '', '', '', '', '', '', '', '')' at line 7

 

 

What have I missed/done wrong?

 

Many  Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/193921-insert-into-problem/
Share on other sites

looks like your form isn't posting as all your variables are empty.

 

add

print_r($_POST);

at the top to make sure you actually have form data coming in.

 

you probably also want to put that block in an if statement so it doesn't execute when there is no form data.

if($_POST['submit_button_name']){//form code here}

 

Link to comment
https://forums.phpfreaks.com/topic/193921-insert-into-problem/#findComment-1020532
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.