Jump to content

ADDING RECORDS TO SQL DATABASE HELP


barryflood22

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

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.