question about php and sql

Ask about a PHP problem here.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

question about php and sql

Post by ta2shop »

dont now if you now, when you send somthing with normal post, thei give you a recipe with a tralink number right ... well i am wonderig if it is posible to make a script with a data base, that for exemple, with a web form type in that number wich is normali here in spain 13 digit long, it will generate you a new one but diferent like "ta2-0123456789" , and i need it to staore the post office number and the random generated one to, so when i lock at it a now this number is for this code?

can this by made with the normal INSERT INTO .. thing or thoes requere someting alse??

P.S: sorry for the looong post :)
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 »

ta2shop wrote:can this by made with the normal INSERT INTO .. thing or thoes requere someting alse??
I don't see why not. What are you actually trying to do though ?

Also as this is an SQL question, moved to the SQl forum :)
Image
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 »

well it is a bit hard for my to explin it so i will start with the html form , i thik a can explain it better like so.
Image
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 »

ok, got stuk on this.
i dont even now how to explain this.
i made the table and the form, it has 2 fields, one for inserting the postal code, and an odher wher the randome code will by displayed.
a do not now how to do the php part so that it will display in that place when the button is presed.
the codes!
html
<div id="wrapper">	
    <div id="top">
    	Generate a Code!
    </div>
	<div id="form">
    	<form action="index.php" method="post">
    		<table align="center" border="0" cellpadding="2" cellspacing="2">
        			<tr align="center">
                		<td>Postal Code</td>
                    	<td>Random Generated Code</td>
                	</tr>
                	<tr align="center">
                		<td><input type="text" name="postal" size="13" class="field" /></td>
                	    <td><input type="text" name="random" size="13" class="field" value="<?php echo $random; ?>" /></td>
                	</tr>
                	<tr align="center">
                		<td colspan="2"><input type="submit" name="generate" value="Generate" class="btn" /></td>
                	</tr>
        	</table>
    	</form>
    </div>
</div>
<div id="lowbox">
<div id="top2">
Serch the Results!
</div>
</div>
i havent made the php part becouse i do not now how :(
thanks.
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 »

well if you want random numbers you can use the mt_rand function. Or see my video on a random password function.
Image
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 »

i already viewd your video, and i am planing on using the
rand(0123456789,9876543210);
the problem is i dont now how to do it.
how to get the random number and how to display it in the input field?
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: question about php and sql

Post by Tino »

Pretty simple. You store it in a variable and display that variable.
<?php
$rand = rand(0. 9); // or whichever numbers you want
?>
<input type="text" value="<?php echo $rand; ?>" />
Please check out my CodeCanyon items.
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 »

Moved to php help now as this is apparently not an SQL question ;)
Image
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 »

i now that , but first i have to type the postal code, and by hiting the gebnerate btn, it will give my the random number in that input field.
and i alsow need to store them both in the DB so i will now witch random code beloncs to what postal code.
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 »

Well do you need the user to be able to edit the random number ? If not you could just use the SQL...
INSERT INTO `table` (`post_code`, `random`) VALUES ('{$post_code}', {$rand});
ie, generating the random number in the backend only,
Image
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 »

i will by the only user, and i need the random code to give it to the customer, so hy can track his order.
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 »

Well you can display the code after the insert. ;)
Image
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 »

ok ok i got somting ... :lol:
now the script gives you a randome code and it is biing stored in the DB, but i cant get it to display in the input field!
the 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']) {
		$query = mysql_query("INSERT INTO postal_codes VALUES('','$postal','$random','$date')") or die(mysql_error());
		echo'<center>Succses, your traker code is: '.$random.' </center>';
	}
	else {
		echo"<center>You must provide a postal code!</center>";
	}
}

?>
any sugestions.
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 »

to display it into a input field just do the following:
<input type='text' name='random' value='<?php echo $rand; ?>'
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 »

Carbine wrote:to display it into a input field just do the following:
<input type='text' name='random' value='<?php echo $rand; ?>'
i alreadi did!
<form action="index.php" method="post">
		<table align="center" border="0" cellpadding="2" cellspacing="2">
			<tr align="center">
				<td>Postal Code</td>
				<td>Random Generated Code</td>
			</tr>
			<tr align="center">
				<td><input type="text" name="postal" size="13" class="field" /></td>
				<td><input type="text" name="random" size="13" class="field" value='<?php echo $random; ?>' /></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input type="submit" name="generate" value="Generate" class="btn" /></td>
			</tr>
		</table>
	</form>
and still nothing
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 »

I don't know if this will do anything but change:
'<?php echo $random; ?>'
to:
"<?php echo $random; ?>"
Sometimes the kind of quotation marks have an effect, that's the only help I can give till a professional comes on and replys :P
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 »

Does it need to be displayed in the input field if the user is told the code afterwards anyway ;)
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 »

Yeah, I was kind of wondering that? If you mean as soon as they press generate and it does it without reloading the page, then you might want to look into javascript.
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 »

:oops: god why am i soo stupid???? :shock:
a was thinking about the design, but that is not important right now :) .
anyway i tried to add a if thing to check if the postal number is alreadi in the DB, and it prints a error.
any ideas:
<?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_'.uniqid(rand(0123456789,9876543210));
$date = date("Y-m-d");

if(isset($_POST['generate'])) {
	if($_POST['postal']) {
		$query = mysql_query("INSERT INTO postal_codes VALUES('','$postal','$random','$date')") or die(mysql_error());
		
		while($row = mysql_fetch_assoc($query)) {
			if($row['postal']!= $postal) {
				echo'<center>Succses, your traker code is: '.$random.' </center>';
			}
			else {
				echo $random .'Already in Use';
			}
		}
	}
	else {
		echo"<center>You must provide a postal code!</center>";
	}
}

?>
thanks
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 »

Well, if you meen to see if the postal is already in use before it is submitted, and if it is in use don't submit the information, I'd do it this way for me:(P.S if it doesn't work, tell me. I just quickly write that, and don't have the databases to test it)
<?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_'.uniqid(rand(0123456789,9876543210));
$date = date("Y-m-d");

if (isset($_POST['generate'])) {
        if($_POST['postal']) {
          $check = mysql_query("SELECT * FROM postal_codes WHERE '$postal'=postal");
          $numrows = mysql_num_rows($check);
          if ($numrows>=1)
            die("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>";
}
?>
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 »

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

also
                $query = mysql_query("INSERT INTO postal_codes VALUES('','$postal','$random','$date')") or die(mysql_error());
               
                while($row = mysql_fetch_assoc($query)) {
                        if($row['postal']!= $postal) {
                                echo'<center>Succses, your traker code is: '.$random.' </center>';
                        }
                        else {
                                echo $random .'Already in Use';
                        }
                }
this is wrong, an INSERT query does not work in the same way as a SELECT, ie you cannot fetch its result.
Image
Post Reply