| | |
Problems with multiple queries
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
I'm working on a script that handles a member sign up. Upon submission of new member info the script queries the users table to check for duplicate user name (in this case an email) and return an error if duplicate is found. If not I want it to just submit the original new member info. The check for duplicates executes okay, but INSERT new info does not.
I thought that maybe assigning a different variable to the INSERT query would work but I had no luck. Also tried to free $results and that didn't work either.
php Syntax (Toggle Plain Text)
$sql = "SELECT * FROM users WHERE username = '{$email}' " ; $result = mysql_query($sql); if ( mysql_num_rows ( $result ) > 0 ) { // Username already exists echo 'That Email is already in use.' ; exit; } else { $sql = "INSERT INTO users (username, password, first_name, last_name) VALUE ('$email', '$pw', '$fname', '$lname')"; echo "Thank you for becoming a member ". $fname . "!"; }
How about:
Although, I tend to do this:
The way I do it ensures that I don't forget a value of put the values in the wrong order. I'm just not organized enough! However it's a bit more difficult to do batch inserts.
PHP Syntax (Toggle Plain Text)
$sql = "INSERT INTO users (username, password, first_name, last_name) VALUES ('{$email}', '{$pw}', '{$fname}', '{$lname}')";
Although, I tend to do this:
PHP Syntax (Toggle Plain Text)
$sql = "INSERT INTO users SET username='{$email}', password='{$pw}', first_name='{$fname}', last_name='{$lname}'";
The way I do it ensures that I don't forget a value of put the values in the wrong order. I'm just not organized enough! However it's a bit more difficult to do batch inserts.
Last edited by ardav; Jul 18th, 2009 at 6:51 pm.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
I must be overlooking something somewhere. I broke the script down to it's simplest form and put it on a page all by itself and it still won't write to the table.
Here is the form page:
And this is the 'write.php' the form submits to:
And of far as I can see everything is fine with the table. I have no problems with anything else writing to other tables in the database.
My table is set up as follows:
table name: users
fields in order:
id > int, auto increment, primary key
username > varchar, 255
password > varchar, 255
first_name > varchar, 255
last_name > varchar, 255
Here is the form page:
PHP Syntax (Toggle Plain Text)
<form action="write.php" type="submit" method="post"> <tr> <td align="left"></td> <td align="left">First Name:</td> <td align="left"><input name="first_name" type="text" /></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left">Last Name:</td> <td align="left"><input name="last_name" type="text" /></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left">Email:</td> <td align="left"><input name="email" type="text" /></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left">Password:</td> <td align="left"><input name="password" type="password" /></td> <td align="left"></td> </tr> <tr> <td colspan="4"><img src="images/spacer_20px_high_white.jpg" /></td> </tr> <tr> <td colspan="4"><div align="center"><input name="create" type="submit" value="Create My Account !" /></div></td> </tr>
And this is the 'write.php' the form submits to:
PHP Syntax (Toggle Plain Text)
<?php $fname = $_POST['first_name']; $fname = ucwords($fname); $lname = $_POST['last_name']; $lname = ucwords($lname); $email = $_POST['email']; $email = strtolower($email); $pw = $_POST['password']; // I omitted my connection info $sql = "INSERT INTO users SET username = '{$email}', password = '{$pw}', first_name = '{$fname}', last_name = '{$lname}' "; if (!$sql) { exit; } echo "Thank you for becoming a member ". $fname . "!"; // Close connection mysql_close($dbcnx); ?>
My table is set up as follows:
table name: users
fields in order:
id > int, auto increment, primary key
username > varchar, 255
password > varchar, 255
first_name > varchar, 255
last_name > varchar, 255
Holy Moley, you'll hate your self for this
You forgot a crucial line:
Regarding the VALUE syntax, VALUE and VALUES are interchangeable.
You forgot a crucial line: mysql_query($sql); which should go on line 16. As your code stands, it doesn't execute the new SQL command, just sets the $sql variable.Regarding the VALUE syntax, VALUE and VALUES are interchangeable.
★ "If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
★ The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
★ Did I help you out? Did I piss you off? Add to my reputation!
★ The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
![]() |
Similar Threads
- dispalying data in grid from multiple queries (VB.NET)
- group by queries in PHP (PHP)
- Unable to preform queries within one button (PHP)
- How many queries can be preformed in one click? (PHP)
- Help with speeding up a query with included sub-queries (MySQL)
- Class Module VB.net Problem (VB.NET)
- How to use multiple inserts with transactions in mysql (PHP)
- Multiple queries within one php file (PHP)
- 200 GB Hard Drive Problems (Windows NT / 2000 / XP)
Other Threads in the PHP Forum
- Previous Thread: Browser Forced Download of Adobe Acrobat .PDF Files.
- Next Thread: how to edit the uploaded file in php???
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





