question about php and sql

Ask about a PHP problem here.
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: question about php and sql

Post by Carbine »

jacek wrote:The above method is correct, although it would be better to use the mysql COUNT() function.

I'm just happy I got it right :P And I haven't quite got round to using the COUNT function yet, I'd rather research it first than just use it and hope for the best :P And I cba to research it atm :P
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: question about php and sql

Post by ta2shop »

thanks guys, it is working now with carabine's method, lock:
[syntax=php]
<?php
$connect = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("ta2shop") or die(mysql_error());

// form data
$postal = $_POST['postal'];
$random = 'TA2_'.rand(0123456789,9876543210);
$date = date("Y-m-d");

if (isset($_POST['generate'])) {
if($_POST['postal']) {
$check = mysql_query("SELECT postal_code FROM postal_codes WHERE '$postal'=postal");
$numrows = mysql_num_rows($check);
if ($numrows!=1)
echo"Postal code has already been used";
else {
$query = mysql_query("INSERT INTO postal_codes VALUES('','$postal','$random','$date')") or die(mysql_error());
echo "<center>Success, your tracker ID is:".$random."</center>";
}
}
else echo "<center>You must provide a postal code!</center>";
}
?>
[/syntax]
but now sql prints this:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\trabajos\ta2shop - tienda\administracion\postal-code\index.php on line 46
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: question about php and sql

Post by Carbine »

Check the query in the '$check' variable and change it to your database details. I didn't know what the fields were, but there is something wrong with the query and that's most likely it.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: question about php and sql

Post by jacek »

[syntax=sql]WHERE '$postal'=postal[/syntax]
This doesn’t look right to me ;)
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: question about php and sql

Post by jacek »

I was thinking more of

[syntax=sql]WHERE `postal` = '$postal_code'[/syntax]
Unsure on the column names though ;)
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: question about php and sql

Post by Carbine »

jacek wrote:I was thinking more of

[syntax=sql]WHERE `postal` = '$postal_code'[/syntax]
Unsure on the column names though ;)

The other way round works on my scripts :P
And it would be [syntax=sql]WHERE 'postal_code' = '$postal'[/syntax]
If you look at the query in the above post by him that looks right.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: question about php and sql

Post by ta2shop »

it is ok, it worck now with no errors.
now i will meke the final check , i hope! the check to see if it is < then 13 or > then 13.
thanks for the help.
Image
Post Reply