Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with my sql statement 1

Status
Not open for further replies.

misslois74

Programmer
Sep 27, 2008
63
PH
im trying to insert into a primary key field which is an alphanumeric which is combination of 'S00' and an incrementing value like for instance: S001, S002, S003 i want it to be system generated...
i have the ff. codes:

$sname = $_POST["supplier_name"];
$saddr = $_POST["supplier_address"];
$scontact = $_POST["supplier_contact"];

$quer = "Select max(substr(supplier_id,-1,1))+1 from supplier";
$row = mysql_query($quer);

$res = "Insert into supplier values(concat('S00',$row),'$sname','$saddr','$scontact')"
mysql_query($res);

what could be the problem with my codes, when i tried the select statement in the MYSQL console its working very fine but once i place it in php and execute it its not adding to the table...
hope anybody could help me with these.
thanks....
 
This is a PHP coding error. The mysql_query function does not return data in the way you are wanting to use it.

Code:
$quer = "Select max(substr(supplier_id,-1,1))+1 from supplier";
$row = mysql_query($quer);
At this point $row is nothing more than a handle. A pointer if you will to the results returned by your query, but it does not actually contain the results.

You need to use one of the row fetching functions such as the mysql_fetch_array function to get access to the rows.

Something along thew lines of:

Code:
$quer = "Select max(substr(supplier_id,-1,1))+1 from supplier";
$res = mysql_query($quer);

$row=mysql_fetch_array($res,MYSQL_NUM);

Then you can access the data returned by the query as:
... concat('S00',$row[[red]0[/red]]) ...


for any more questions regarding the proper PHP coding of such things please post in the PHP forum here:

forum434




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks a lot for all the help now its working properly....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top