Current syntax error when connecting to version 5.0 mysql - php

I am really having a tough time understanding what syntax to use. I am VERY green when it comes to coding. I copied this code from another website that is working just fine but they are hooked up to a 4.3 version of mysql- can anyone help?This is the 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 '1, city, state, zip, ) VALUES ('', , ' 1', '', '', '','' )' at line 1
Here is my code:
mysql_select_db("membership70", $con);
$name=mysql_real_escape_string($_POST['Name']);
$address1=mysql_real_escape_string($_POST['Address1']);
$city=mysql_real_escape_string($_POST['city']);
$state=mysql_real_escape_string($_POST['state']);
$zip=mysql_real_escape_string($_POST['zip']);
$email=mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}

You have speces in address [space] 1 both places.
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
Also you have an extra colon after zip and the order of the values does not match the order you gave the column names. Try:
$sql="INSERT INTO Members (name, email, adress1, city, state, zip) VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip')";

Get rid of the space in adress 1 and your variable $adress 1....not allowed (spaces that is)

That statement clearly tells you the SQL syntax is broken. Take a closer look into your statement in the $sql variable. echo it to debug it properly.
$sql = "INSERT INTO .....";
echo $sql; die();
At first glace, there shouldn't be a blank neither the column adress nor the corresponding php variable. And you should sort the order of your columns. email is declare second, so don't put the value of it at the end.
So, instead of...
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
...try...
$sql="INSERT INTO Members (name, email, adress1, city, state, zip) VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip' )";

You have a column adress 1 there, that's not a valid column name in mysql and it could not have worked on any mysql version. What columns does your table have?
Also, your second value is empty, put some value in there (or a default one). And a comma at the end of the columns list. Email at the wrong place.
Maybe this would work?
$sql = "INSERT INTO Members (name, email, adress1, city, state, zip) ".
"VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip')";
(I'm not sure about the third column, should it spell address1?)

Related

My insert query only executes once

I have a registration page that works perfectly fine when it the first user registers. When the second user registers the INSERT query outputs an error. Here is my code:
if ($err_found != true)
$saveData = "INSERT INTO anc_user_info (CardNumber, UserName, Password, Name,
Surname, IDNumber, Province, Region, Branch, Gender, Language,
PhysicalAddress, Profession, Category, TelNoW, TelNoH, Email,
Cell, ProfilePic, TransactionRef, DepositAmount, DepositerName,
Declaration, UserID, Status, CreateDate)
VALUES( '$CardNumber', '$UserName', md5('$Password'), '$Name', '$Surname',
'$IDNumber', '$Province', '$Region', '$Branch', '$Gender',
'$Language', '$PhysicalAddress', '$Profession', '$Category',
'$TelNoW', '$TelNoH', '$Email', '$Cell', '$ProfilePic',
'$TransactionRef', '$DepositAmount', '$DepositerName',
'$Declaration', '$UserID', 0, NOW())" ;

PHP-MySQL INSERT Operation isn't Working

I'm developing a web platform. I'm using PHP and MySQL. I want to insert data to db. My code below.
<?php
session_start();
require_once('../../system/database.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ownerid = (int)$_SESSION['id'];
$record_time = date('H:i:s');
$record_date = date('Y-m-d');
// form_data
$name = strip_tags($_POST['name']);
$surname = strip_tags($_POST['surname']);
$phone1 = strip_tags($_POST['phone1']);
$birthday = strip_tags(trim($_POST['birthday']));
$gender = strip_tags(trim($_POST['gender']));
$company = strip_tags(trim($_POST['company']));
$address1 = strip_tags(trim($_POST['address1']));
$address2 = strip_tags(trim($_POST['address2']));
$phone2 = strip_tags(trim($_POST['phone2']));
$mail1 = strip_tags(trim($_POST['mail1']));
$mail2 = strip_tags(trim($_POST['mail2']));
$about = strip_tags(trim($_POST['about']));
$type_of = strip_tags(trim($_POST['type_of']));
$visible = strip_tags(trim($_POST['visible']));
$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1', '$phone2', '$mail1', '$mail2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";
$result = mysqli_query($connection, $query);
mysqli_error($connection);
} else {
header('Location: ../new_contact.php');
}
But my MySQL code does not work and write any error message!
wrong number of column in values clause (you repeat two time mail1 and mail2 ) try
$query = "INSERT INTO contact
(ownerid, name, surname, birthday, gender, address1, address2, phone1,
phone2, mail1, mail2, about, type_of, visible, time, date)
VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1',
'$phone2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";
Edit insert variables to be like this
$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES(".$ownerid.", '".$name."', '".$surname."', '".$birthday."', '".$gender."', '".$address1."', '".$address2."', '".$phone1."', '".$phone2."', '".$mail1."', '".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";
You need to concate PHP variable properly and also need correct query format. Try this
$query = "INSERT INTO `contact` (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('".$ownerid."','".$name."', '".$surname."', '".$birthday."','".$gender."', '".$address1."', '".$address2."', '".$phone1."','".$phone2."', '".$mail1."','".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";
If you have access to PHPMyAdmin, place the query in the SQL section and test it. Sometimes it can give you clues as where to look for issues.
As ScaisEdge says, you have 16 entries as keys, but 18 entries as values into your insert. It can be helpful to use coding software when using PHP that highlights like words (notepad++ is free and does this) Eclipse is another. Also, place your query into PHPMyAdmin "SQL" and test your query to see if it gives you any results or throws an error. It will point where in the string to start looking for your error.

Difficulty processing form with IP

Having a difficulty involving a form that wants the user to fill in his/her IP.
Gives the following error;
Warning: pg_query(): Query failed: ERROR: syntax error at or near
"'192.192.192.192/32'" LINE 1: ...ess, ip, status) VALUES ('1',
'TestFirstName', 'TestLastName' '192.192.1... ^ in
/Applications/XAMPP/xamppfiles/htdocs/LoginHQ/functions.php on line 22
Apparentely, something is going wrong with the IP form field that gets sent through a POST.
My query is as following;
$result = pg_query($dbconnection, "INSERT INTO clients(clientid, firstname, lastname, ipaddress, ip, status) VALUES ('$clientid', '$FirstName', '$LastName' '$ip', '$status')");
Is there something wrong with this formatting, or should I dig deeper?
You missed a comma between '$LastName' and '$ip'.Correct your query like this
$result = pg_query($dbconnection, "INSERT INTO clients(clientid, firstname, lastname, ipaddress, ip, status) VALUES ('$clientid', '$FirstName', '$LastName', '$ip', '$status')")
You're missing a , between '$LastName' and '$ip'.
You forgot a comma (,) between '$LastName' and '$ip'.

Column Count vs Value Count on insert statement - PHP/MySQL

I am working on building an employee database but seem to have run into an interesting issue.
When I run my query to add a new user/employee, I get the error:
Column count doesn't match value count at row 1
From what I have researched, this seems to be an error with inserting more/less values than what is declared in the first part of an insert statement example:
INSERT INTO table (col1, col2) VALUES (val1, val2, val3)
The thing is though, I have looked over my query and the columns and values match perfectly (count wise). I have even looked for things in my query such as missing quotes, commas, etc.
Here is my code (query):
$db->query("INSERT INTO userdata (
Username,
Email,
Phone,
Password,
FirstName,
LastName,
Address,
City,
State,
Zip,
JobTitle,
UserGroup,
JobStatus,
Points,
Status,
BirthDate,
HireDate,
HourlyRate,
DateAdded,
SSN
) VALUES (
'$Username',
'$Email',
'$Phone',
'$Password',
'$FirstName',
'$LastName',
'$Address',
'$City',
'$State',
'$Zip',
'$JobTitle',
'$Group',
'$JobStatus',
0,
'$Status',
'$BirthDate',
'$HireDate',
'$HourlyRate'
'$TodaysDate',
'$SSN'
)") or die(mysqli_error($db));
Some things to note:
This not all of the columns in the table have data inserted here (I think its possible to do this and things such as auto incrementing ID's will fill themselves in and others will be left blank)
From the variable dumps I have done, all of these variables are valid.
I am really confused about this and any help would be appreciated.
Check the following portion again:
INSERT INTO userdata(...,
JobStatus,
UserGroup,
Points,
UserGroup,
Status,
.....
,)VALUES(...,
$JobStatus',
0,
'$Group',
'$Status',
......
)
In values(, ?, ?, ?), after jobstatus, there should be UserGroup and then Points. And UserGroup appeared twice.

SQL - Why is my INSERT query dying?

echo "</br></br></br>" . $sql;
mysql_query($sql) or die("Entry not added to database.");
is spitting out:
INSERT INTO potentials (id, firstname, lastname, age, email, phone, twitter, timeofday, dayofweek, address, city, state, zip, joindate, parentname, parentnumber) VALUES (null, 'Rick', 'Bross', '14', 'rbross3#gmail.com', '8164896991', '#rick_bross', 'After 5:30PM', 'Weekdays', '1234 Cooper', 'Raymore', 'MO', '64130', '2013-04-09 20:10:06', 'Rick Bross II', '1234123412')Entry not added to database.
Why isn't it being inserted correctly? Can I check if one of the strings isnt fitting into my database column (type type char error or something)?
mysql_query($sql) or die(mysql_error());
try msql_error() function. It is globally show every error happens with your sql execution

Categories