How to get my mysq_fetch_array code to work in PHP

Posted on 2009-04-16
Last Modified: 2013-12-12
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.

<body bgcolor=#998855>


//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


$job = $_POST["job"];

$description = $_POST["description"];

$cost = $_POST["cost"];




    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());





  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 />";






Open in new window

Question by:dscits

    Accepted Solution

    Never mind. I figured it out. I had an extra ) in my mysql_query statement.
    LVL 39

    Expert Comment

    by:Roger Baklund
    There is a special function you can use for this:
        mysql_query("insert into `jobs` values(NULL,'$job')") or die (mysql_error());
      echo "Please fill out the Job Name field.";

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now