• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

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

0
dscits
Asked:
dscits
1 Solution
 
dscitsAuthor Commented:
Never mind. I figured it out. I had an extra ) in my mysql_query statement.
0
 
Roger BaklundCommented:
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now