Jump to content

query from a textbox


daniel0816

Recommended Posts

I am trying to write a query that executes when the user types in a number into a textbox and then clicks the search button. The query is supposed to get the JS_Status from the JobStatus table with the JS_ID that matches the value that has been submitted through the textbox and display it in an alert box. The code I have runs but it gives me this weird message in the alert box saying Resource id#3 which I am hoping someone could tell me what that is.

 

Here is my code:

 

 

<?php
error_reporting(E_ALL);
$connect = mysql_connect("dbinfo", "dbinfo", "dbinfo");
//select database
mysql_select_db("dbinfo", $connect);

$refNum = $_POST["refNum"];
$status = mysql_query("SELECT JS_Status FROM JobStatus WHERE $refNum = JS_ID");
if($status)
{
 
 echo '<script language="javascript" type="text/javascript">
 alert(\''.$status .'\');
 </script>';
}else
 {
  die ("could not run query");
 }

?>

 

Thanks again

 

Link to comment
Share on other sites

Have you tried echoing $status on the PHP side to see what it contains?

 

Side note: perhaps you were going to do this later, but it's recommended that you sanitize/validate any user-supplied information before using it in things like MySQL queries. Since $_POST["refNum"] is supposed to be a number, you could utilize ctype_digit():

http://php.net/manual/en/function.ctype-digit.php

Link to comment
Share on other sites

When you execute a query, the return value is a resource identifier that points to the results of the query. You need to use one of the mysql_fetch functions to get the actual data. And, that data would be an array - so it would not be usable in JavaScript until you get the value you need from the array first. The less used method would be to mysql_result() to get a particular value from the resource identifier.

 

 

<?php

error_reporting(E_ALL);
$connect = mysql_connect("dbinfo", "dbinfo", "dbinfo");

//select database
mysql_select_db("dbinfo", $connect);

$refNum = intval($_POST["refNum"]);
$query = "SELECT JS_Status FROM JobStatus WHERE JS_ID = $refNum";
$result = mysql_query($query);

if(!$result)
{
    $message = "There was a problem getting the results";
}
elseif(!mysql_num_rows($result))
{
    $message = "There was no matching record";
}
else
{
    $status = mysql_result($result, 0);
}

echo "<script language=\"javascript\" type=\"text/javascript\">
    alert('{$status}');
</script>";

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.