Page 1 of 1

Mysql insert... nothing happens no errors

Posted: Tue Jul 24, 2007 12:13 am
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]

Posted: Tue Jul 24, 2007 3:35 am
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());

Posted: Tue Jul 24, 2007 7:07 am
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)

Posted: Wed Jul 25, 2007 1:53 pm
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...

Posted: Wed Jul 25, 2007 2:31 pm
by miro_igov
sure, values must be surrounded by single quotes ... VALUES ( 'Eric', 'Edited', etc

Otherwise they are considered as mysql columns.

Posted: Wed Jul 25, 2007 3:37 pm
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.

Posted: Thu Jul 26, 2007 12:46 am
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.

Posted: Thu Jul 26, 2007 6:43 am
by miro_igov
Totally wrong. Must be like this

Code: Select all

VALUES ('" . $fname ."', '".$lname."','" . $username ."',
and so

Posted: Thu Jul 26, 2007 12:56 pm
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

Posted: Thu Jul 26, 2007 1:41 pm
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!';
}
?>

Posted: Thu Jul 26, 2007 3:32 pm
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.

Posted: Thu Jul 26, 2007 3:49 pm
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.

Posted: Thu Jul 26, 2007 3:52 pm
by miro_igov
Hmm yes, true. but pasting the query in a mysql client would help him finding the issue.

Posted: Thu Jul 26, 2007 5:53 pm
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

Posted: Thu Jul 26, 2007 6:23 pm
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.