Link to home
Start Free TrialLog in
Avatar of dscits
dscits

asked on

How to get my mysq_fetch_array code to work in PHP

I'm trying to see what the id of the last row of a table is in MySQL. Then, I want to use that id number to insert it into another table. My bidding info table has four fields: id, jid, description, cost. My jobs table has two fields: id and name. I'm trying to retrieve the id from jobs and then insert it into the jid of bidding info. That way, the description and cost fields have a corresponding job id that links to the job name in jobs. I think there is something wrong with my mysql_fetch_array line that looks like this: $tmp = mysql_fetch_array($result) or die(mysql_error());
I'm getting an error that the mysql_fetch_array command is wrong. I'm not sure what I'm doing wrong. Any help would be greatly appreciated.
<html>
 
<body bgcolor=#998855>
 
<?php
 
//this includes the host, username, password, etc for db connection
include ('dbcons2.php');
 
//This calls the db_connect function from the above include called dbcons.php
db_connect();
 
$job = $_POST["job"];
$description = $_POST["description"];
$cost = $_POST["cost"];
$jid=0;
 
if($job)
  {
    mysql_query("insert into `jobs` values('NULL','$job')") or die (mysql_error());
    $result = mysql_query("select id from `jobs` order by id desc limit 1)");
    $tmp = mysql_fetch_array($result) or die(mysql_error());
    $jid=$tmp[id];
    }
else
{
  echo "Please fill out the Job Name field.";
}
 
//Pass All Data From Form
for($i=1;$i<=count($_POST['description']); $i++)
{
  if($description[$i]!='' && $cost[$i]!='')
    {
 
    mysql_query("insert into `bidding info` values('NULL','$jid','$description[$i]','$cost[$i]')") or die (mysql_error());
 
    echo "Your form information was sent.<br /><br />
    <a href='bid.php'>Back to Bid Proposal Page</a><br /><br />";
    }
 
}
 
?>
 
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dscits
dscits

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is a special function you can use for this:
if($job)
  {
    mysql_query("insert into `jobs` values(NULL,'$job')") or die (mysql_error());
    $jid=mysql_insert_id();
    }
else
{
  echo "Please fill out the Job Name field.";
}

Open in new window