Jump to content

what is wrong


dignifiedman

Recommended Posts

ok i spent over 3 days trying to get this shitty piece of code to work ready to throw away my pc and live in a cave for rest of my life.

 

please tell me why it doesnt work, all i want is for it to add the previouse page's form too my database, is that really too much to ask? theres not even any validation on there yet?

 

<?php

$db = mysql_connect ('mysql8.000webhost.com', 'a3523162_15cred', 'holylight22');

mysql_select_db('a3523162_15cred', $db) or die ('Unable to connect to database');

$sql="INSERT INTO Customers (CustomerID, EmailAddress, Postalcode, Phonenumber, Forename, Surname, Address)
VALUES ($_POST['CustomerID'], $_POST['EmailAddress'], $_POST['Postalcode'], $_POST['Phonenumber'], $_POST['Forename'], $_POST['Surname'], $_POST['Address']);

if (!mysql_query($sql,$db))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
echo "Record Added<br><a href=\"index.html\">click here</a>
to return to sysops<br>";
mysql_close($db);
?>

<p><a href="Check Your booking.php">View</a></p>

Link to comment
https://forums.phpfreaks.com/topic/206984-what-is-wrong/
Share on other sites

Hi,

 

First look the cause of the error is the insert statement. Try this:

 

$sql="INSERT INTO Customers (CustomerID, EmailAddress, Postalcode, Phonenumber, Forename, Surname, Address)
VALUES ($_POST['CustomerID'], \"$_POST['EmailAddress']\", $_POST['Postalcode'], $_POST['Phonenumber'], \"$_POST['Forename']\", \"$_POST['Surname']\", \"$_POST['Address']\");";

 

If phonenumber is a VARCHAR then: \"$_POST['Phonenumber']\"

Link to comment
https://forums.phpfreaks.com/topic/206984-what-is-wrong/#findComment-1082336
Share on other sites

i now have this -

 

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 ' , , yuhkyfu, yuiruir, yuiruiru)' at line 2

 

$sql="INSERT INTO Customers (CustomerID, EmailAddress, Postalcode, Phonenumber, Forename, Surname, Address)
VALUES ({$_POST['CustomerID']}, {$_POST['EmailAddress']}, {$_POST['Postalcode']}, {$_POST['Phonenumber']}, {$_POST['Forename']}, {$_POST['Surname']}, {$_POST['Address']})";

 

Link to comment
https://forums.phpfreaks.com/topic/206984-what-is-wrong/#findComment-1082393
Share on other sites

To ensure the SQL string is properly encapsulated in quotes where necessary and to avoid any post confusion, I would personally do it this way

 

$sql = "INSERT INTO Customers (CustomerID, EmailAddress, Postalcode, Phonenumber, Forename, Surname, Address)
VALUES ('" . $_POST['CustomerID'] . "', '" . $_POST['EmailAddress'] . "', '" . $_POST['Postalcode'] . "', '" . $_POST['Phonenumber'] . "', '" . $_POST['Forename'] . "', '" . $_POST['Surname'] . "', '" . $_POST['Address'] . "')";

 

So if the Values were as follows:

 

CustomerID: 10976

EmailAddress: [email protected]

Postalcode: L1H1A4

PhoneNumber: 4164770101

Forename: Bob

Surname: Smith

Address: 123 Front St, Toronto

 

The generated MySQL would be:

 

INSERT INTO Customers (CustomerID, EmailAddress, Postalcode, Phonenumber, Forename, Surname, Address) VALUES ('10976', '[email protected]', '4164770101', 'Bob', 'Smith', '123 Front St, Toronto');

 

You may want to make some adjustments. For example, if the CustomerID field is an INT value, then you can safely add that field without the single quotations  '.

Also, you may want to mysql_escape_string or whatever the function is called to add \ to each post value prior to inserting. So if you had someone with the name Mc'Leans for example, the resulting insert for that field would be 'Mc\'Leans'

 

Link to comment
https://forums.phpfreaks.com/topic/206984-what-is-wrong/#findComment-1082423
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.