Jump to content

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: bob@bob.com

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', 'bob@bob.com', '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

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.