Mysql insert... nothing happens no errors

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
X_Citer
Forum Newbie
Posts: 10
Joined: Thu Jul 12, 2007 11:18 pm

Mysql insert... nothing happens no errors

Post by X_Citer »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


for some reason the following piece of code is giving my hosting business a run for its money... clients (or developers at this stage) input all their information into a form which is parsed and the results are entered into a mysql db.. which is remote but no access problems as of now.  i checked the phpmyadmin... table isnt created... nothing happens when you submit except that it brings back up the blank signup sheet.

Code: Select all

<?php
mysql_select_db("woodenwe_clients", $con);
	  
$uid=mysql_query("SELECT 'uid' FROM 'accounts'");
$uid_results=mysql_num_rows($uid);
$new_uid=($uid_results+1);
	  
	  
	  mysql_query("INSERT INTO 'accounts' firstname, lastname, phone, username, password, street, city, state, zip, email, class, uid, domain, plan, date, status VALUES ".$fname, $lname, $username, $password,$street, $city, $state, $zip, $email, $resell, $class, $new_uid, $domain, $plan, $date, "inactive");
	  
	  mysql_close($con);
	  
	  mail($email, "'Welcome to Wooden Web Hosting', 'Welcome to Wooden Web Hosting.<p>You can activate your account by clikcing the link below.  Once activated, a sales representative will assist you via live chat or telephone at your convenience.<p><a href='http://woodenwebhosting.co.nr/signup/activate.php?uid='$new_uid></a>'");
	  
	  print "Check your email for information about account activation.";
	
	  }
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Instead of $new_id you may use auto_increment field. And reading the mysql_error() may help you find the error.

Code: Select all

$uid=mysql_query("SELECT 'uid' FROM 'accounts'") or die(mysql_error());
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Seems you forgot the brackets around the fields set
Proper MySQL insert code:

Code: Select all

INSERT INTO table (a,b,c) VALUES (1,2,3)
X_Citer
Forum Newbie
Posts: 10
Joined: Thu Jul 12, 2007 11:18 pm

Post by X_Citer »

Thanks i updated the table to use auto_increments however it still doesnt allow me to insert.. ive taken out all the variables to test straight text. Here is the line of code and the error it gives me... ive tried every combination of syntax i can think of to no avail the mysql version is 5.0.27.

<?php
mysql_query("INSERT INTO accounts (firstname, lastname, phone, username, password, street, city, state, zip, email, class, domain, plan, date, status) VALUES (Eric, edited, xxx-xxx-xxxx, X_Citer, mypass, where i live, Richmond, Ky, 40475, myemail@somewhere.com, 5, woodenwebhosting.com, admin, 07-24-07, active)") or die(mysql_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 '[(firstname, lastname, phone, username, password, street, city, state, zip, emai' at line 1

I am out of ideas guys...
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

sure, values must be surrounded by single quotes ... VALUES ( 'Eric', 'Edited', etc

Otherwise they are considered as mysql columns.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

If you echo your query, you would see that..

Code: Select all

$fname, $lname, $username, $password,$street, $city, $state, $zip, $email, $resell, $class, $new_uid, $domain, $plan, $date,
is the same as..

Code: Select all

$fname . $lname . $username . $password . $street . $city . $state . $zip . $email . $resell . $class . $new_uid . $domain . $plan . $date
So the query will clearly fail as your trying to shove 17 unescaped fields into 1.
X_Citer
Forum Newbie
Posts: 10
Joined: Thu Jul 12, 2007 11:18 pm

Post by X_Citer »

I've changed my query message with the variables to this...

Code: Select all

mysql_query("INSERT INTO accounts (firstname, lastname, phone, username, password, street, city, state, zip, email, class, domain, plan, date, status) VALUES (" . $fname . $lname . $username . $password . $street . $city . $state . $zip . $email . $class . $domain . $plan . $date . "'inactive')");
i get nothing... though the syntax you guys advised me to use worked with my specific information.. i dont think im getting what you mean by "shoving 17 encapsed fields into 1.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Totally wrong. Must be like this

Code: Select all

VALUES ('" . $fname ."', '".$lname."','" . $username ."',
and so
X_Citer
Forum Newbie
Posts: 10
Joined: Thu Jul 12, 2007 11:18 pm

Post by X_Citer »

Thanks for the tip... i guess that im hopeless because when i changed the above section to this...

Code: Select all

mysql_query("INSERT INTO accounts (firstname, lastname, phone, username, password, street, city, state, zip, email, class, domain, plan, date, status) VALUES ('".$fname."', '".$lname."', '".$username."', '".$password ."', '".$street."', '".$city."', '".$state."', '".$zip."', '".$email."', '".$class."', '".$domain."', '".$plan."', '".$date."', 'inactive')") or die(mysql_error());
still nothing happens...
I've tried combinations of ."','". moved spaces around and such... but its still giving me nothing
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$sql = "INSERT INTO 
            `accounts` (
                `firstname`, 
                `lastname`, 
                `phone`, 
                `username`, 
                `password`, 
                `street`, 
                `city`, 
                `state`, 
                `zip`, 
                `email`, 
                `class`, 
                `uid`, 
                `domain`, 
                `plan`, 
                `date`, 
                `status` 
            )
            VALUES ( 
                '$fname',
                '$lname', 
                '$username', 
                '$password',
                '$street', 
                '$city', 
                '$state', 
                '$zip', 
                '$email', 
                '$resell', 
                '$class', 
                '$new_uid', 
                '$domain', 
                '$plan', 
                '$date', 
                'inactive'
            )";
if (mysql_query($sql) === false || !mysql_affected_rows()) {
    die('There was an error in the query:<br />' . $sql . '<br /><br />' . mysql_error());
} else {
    echo 'The query worked!';
}
?>
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

When i make some script and it does strange with no reason i usually do debug and quickly find the issue. In your case you can

Code: Select all

print "INSERT INTO accounts (firstname, lastname, phone, username, password, street, city, state, zip, email, class, domain, plan, date, status) VALUES ('".$fname."', '".$lname."', '".$username."', '".$password ."', '".$street."', '".$city."', '".$state."', '".$zip."', '".$email."', '".$class."', '".$domain."', '".$plan."', '".$date."', 'inactive')"
and then paste the query into mysql client.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

miro_igov wrote:When i make some script and it does strange with no reason i usually do debug and quickly find the issue. In your case you can

Code: Select all

print "INSERT INTO accounts (firstname, lastname, phone, username, password, street, city, state, zip, email, class, domain, plan, date, status) VALUES ('".$fname."', '".$lname."', '".$username."', '".$password ."', '".$street."', '".$city."', '".$state."', '".$zip."', '".$email."', '".$class."', '".$domain."', '".$plan."', '".$date."', 'inactive')"
and then paste the query into mysql client.
Why are you concatenating that much. Why not let the string get parsed in this case?

Code: Select all

$sql = "INSERT INTO `accounts` (`firstname`, `lastname`, `phone`, `username`, `password`, `street`, `city`, `state`, `zip`, `email`, `class`, `domain`, `plan`, `date`, `status`) VALUES ('$fname', '$lname', '$username', '$password', '$street', '$city', '$state', '$zip', '$email', '$class', '$domain', '$plan', '$date', 'inactive')";
print $sql;
Also, why is no one else telling this poster to use backticks around the field names. If I remember correctly, "date" is a reserved word in MySQL (as a few other field names you got going on there I think). Not to mention that it is just good coding practice to wrap field names, table names and database names in backticks.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Hmm yes, true. but pasting the query in a mysql client would help him finding the issue.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Everah wrote:Also, why is no one else telling this poster to use backticks around the field names. If I remember correctly, "date" is a reserved word in MySQL (as a few other field names you got going on there I think). Not to mention that it is just good coding practice to wrap field names, table names and database names in backticks.
Finally, someone agrees with me. :-D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

miro_igov wrote:Hmm yes, true. but pasting the query in a mysql client would help him finding the issue.
Ok, but that has nothing to do with that mess of code used to generate the query in the first place. Ultimately it all becomes a string that MySQL needs to be able to interpret as a query.
Post Reply