Page 2 of 2
Re: question about php and sql
Posted: Sat May 07, 2011 3:09 pm
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
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
And I cba to research it atm
Re: question about php and sql
Posted: Sat May 07, 2011 3:25 pm
by ta2shop
thanks guys, it is working now with carabine's method, lock:
<?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>";
}
?>
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
Re: question about php and sql
Posted: Sat May 07, 2011 3:29 pm
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.
Re: question about php and sql
Posted: Sat May 07, 2011 3:30 pm
by jacek
WHERE '$postal'=postal
This doesn’t look right to me
Re: question about php and sql
Posted: Sat May 07, 2011 3:33 pm
by jacek
I was thinking more of
WHERE `postal` = '$postal_code'
Unsure on the column names though
Re: question about php and sql
Posted: Sat May 07, 2011 3:36 pm
by Carbine
jacek wrote:I was thinking more of
WHERE `postal` = '$postal_code'
Unsure on the column names though
The other way round works on my scripts
And it would be
WHERE 'postal_code' = '$postal'
If you look at the query in the above post by him that looks right.
Re: question about php and sql
Posted: Sat May 07, 2011 3:56 pm
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.