Jump to content

Recommended Posts

I have created a form that will input new records to an sql table, but it wont work. the database connects fine, iv already tested that, but for some reason i cant get it to add new records to the table. please help!!!

 

 

 

---form page----

 

 

 

<form action="insert.php" method="post">

Name: <input type="text" name="Name"><br>

Address: <input type="text" name="Address"><br>

Postcode: <input type="text" name="Postcode"><br>

Telephone Number: <input type="text" name="Telephone_Number"><br>

Email Address: <input type="text" name="Email_Address"><br>

Type: <input type="text" name="Type"><br>

Appointment: <input type="text" name="Appointment"><br>

<input type="Submit">

</form>

 

 

 

DATA FROM ABOVE WILL NOW IMPORT TO THE BELOW PAGE

 

 

--- insert page ----

<?

$username="admin";

$server="localhost";

$password="admin";

$database="hoff_real_estate";

 

 

$Name=$_POST['Name'];

$Address=$_POST['Address'];

$Postcode=$_POST['Postcode'];

$Telephone_Number=$_POST['Telephone_Number'];

$Email_Address=$_POST['Email_Address'];

$Type=$_POST['Type'];

$Appointment=$_POST['Appointment'];

$query = "INSERT INTO "customers" VALUES ('$Name','$Address','$Postcode','$Telephone_Number','$Email_Address','$Type','$Appointment')";

 

 

mysql_connect($server,$username,$password) or die( "Unable to select table");

 

@mysql_select_db($database) or die( "Unable to select database");

 

 

mysql_query($query);

 

mysql_close();

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/46301-adding-records-to-sql-database-help/
Share on other sites

<?php
$query = "INSERT INTO customers (Name, Address , Postcode, Telephone_Number, Email_Address, Type, Appointment) VALUES ('$Name','$Address','$Postcode','$Telephone_Number','$Email_Address','$Type','$Appointment')";
?>

 

Name, Address etc are the field name in your database.

Your original error was that you had double quotes around customers.

$query = "INSERT INTO customers VALUES ('$Name','$Address','$Postcode','$Telephone_Number','$Email_Address','$Type','$Appointment')";

 

It is preferred to list out the field name for which the values are to be inserted.

 

INSERT INTO table (fieldname1, fieldname2, fieldname3) VALUES ('value1', 'value2', 'value3');

 

iv changed it to that, but its still not working.

 

<?
$username="admin";
$server="localhost";
$password="admin";
$database="hoff_real_estate";


$Name=$_POST['Name'];
$Address=$_POST['Address'];
$Postcode=$_POST['Postcode'];
$Telephone_Number=$_POST['Telephone_Number'];
$Email_Address=$_POST['Email_Address'];
$Type=$_POST['Type'];
$Appointment=$_POST['Appointment'];
$query = "INSERT INTO customers (Name, Address , Postcode, Telephone_Number, Email_Address, Type, Appointment) VALUES ('$Name','$Address','$Postcode','$Telephone_Number','$Email_Address','$Type','$Appointment')";


mysql_connect($server,$username,$password) or die( "Unable to select table");

@mysql_select_db($database) or die( "Unable to select database");


mysql_query($query);

mysql_close();
?>

Do the field names actually match those in your database?? I guessed them from the naming of you variables.

 

Try replace the following into your code and see what errors you get.

<?php
mysql_connect($server,$username,$password) or die(mysql_error());

@mysql_select_db($database) or die(mysql_error());


mysql_query($query) or die(mysql_error());

mysql_close();
?>

i ran this :

 

<?

$username="admin";

$server="localhost";

$password="admin";

$database="hoff_real_estate";

 

 

$Name=$_POST['Name'];

$Address=$_POST['Address'];

$Postcode=$_POST['Postcode'];

$Telephone_Number=$_POST['Telephone_Number'];

$Email_Address=$_POST['Email_Address'];

$Type=$_POST['Type'];

$Appointment=$_POST['Appointment'];

$query = "INSERT INTO customers (Name, Address , Postcode, Telephone_Number, Email_Address, Type, Appointment) VALUES ('$Name','$Address','$Postcode','$Telephone_Number','$Email_Address','$Type','$Appointment')";

 

 

 

mysql_connect($server,$username,$password) or die(mysql_error());

 

@mysql_select_db($database) or die(mysql_error());

 

 

mysql_query($query) or die(mysql_error());

 

mysql_close();

 

?>

 

 

and it didnt make a difference

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.