Jump to content

mysql & php! finding an error, help!


iamnothyper

Recommended Posts

Hi!

 

I'm doing an assignment for school and I need help finding the error in my code. It's a two part process where a php form grabs the data and inserts the result into mysql. i'll try to only post up the bits and pieces that are relevant.

 

here's the form part. as far as i know this part is working fine, but it might be helpful to see it? 

$query2 = "SELECT ticker FROM tickers ORDER BY ticker ";
$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
 
print("Stock Ticker:<br />");
print("<select name=ticker>"); 
 
while ($line2 = mysql_fetch_array($result2))   {
    print("<option value=\"$line2[0]\">$line2[0]</option>\n");
}
print("</select>");

here is the result part. its linked via: <form action="transaction_added.php" method="post">

$ticker    = $_POST['ticker']; 
$dataCheck = 1;
 
if ($dataCheck > 0) 
{
 
/* Performing SQL SELECT query */
$query1 = "SELECT close FROM stocks WHERE the_date = $the_date AND ticker = $ticker ";
$price = mysql_query($query1) or die("Query failed : " . mysql_error());

the error message i'm getting on the webpage is: Query failed : Unknown column 'JCP' in 'where clause'

JCP is the ticker value in this case. I'm not sure why it's taking JCP to be the column name? What am I doing wrong here?

 

Thanks!
Link to comment
Share on other sites

Add single quotes around $the_date and $ticker to indicate that they are strings. Then tell me what happens.

hi!

thanks, that stopped the JCP error but when I do this to check

print("\t Price of Stock: $price <br>\n");

I get "Price of Stock: Resource id #4" ?

How do i get it to print the actual price? 

 

i'm also getting the same problem for $fullname and $buysell. i thought setting them equal to query stores the value?

$query2 = "SELECT CONCAT(fname,' ',lname)FROM clients WHERE client_ID = '$client_ID' ";
$fullname = mysql_query($query2) or die("Query failed : " . mysql_error());

$query3 = "SELECT CASE buy WHEN 0 THEN 'Sell' WHEN 1 THEN 'Buy' END FROM transactions ";
$buysell = mysql_query($query3) or die("Query failed : " . mysql_error());

and i'm getting this error message at the bottom of my screen.

Query failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id #4,1,1)' at line 2
Edited by iamnothyper
Link to comment
Share on other sites

Read the manual page for mysql_query. It has sample code to follow.

 

i looked and no matter what variations of fetch/assoc i try to use i cant seem to get it working. either i break the code or nothing gets printed at all, or just "resource id#4"

Link to comment
Share on other sites

Post what you tried. Your code didn't have any fetch before.

 

i did this

 

$query1 = "SELECT close FROM stocks WHERE the_date = '$the_date' AND ticker = '$ticker' ";
$result1 = mysql_query($query1) or die("Query failed : " . mysql_error()); 
$price = mysql_fetch_assoc($result1);


$query2 = "SELECT CONCAT(fname,' ',lname)FROM clients WHERE client_ID = '$client_ID' ";
$result2 = mysql_query($query2) or die("Query failed : " . mysql_error()); 
$fullname = mysql_fetch_assoc($result2);
and now i get an undefined variable error.

i need to insert $price later and print out both $price & $buysell

Edited by iamnothyper
Link to comment
Share on other sites

Jessica, on 04 May 2013 - 6:09 PM, said:

What error?

Did you try any debugging?

it just says Undefined variable price/buysell/fullname... etc etc on the webpage when i try to print it

 

what i want to do ultimately is insert the value $price into a table and print out the values for all of them. $price comes from a query of two variables taken from a php form on a previous page. and $fullname/$buysell are supposed to be kinda link concats of two other variables so i can print the info for the user?

 

Debugging? well not really because my only problem is with how i'm handling these variables i think?

 

iunno, im just really confused at this point >< thanks for helping me!

Edited by iamnothyper
Link to comment
Share on other sites

Jessica, on 04 May 2013 - 6:29 PM, said:

If you keep only showing bits and pieces and refusing to post actual errors with line numbers, this will take forever. Because I will go away.

this is the entire code, i changed some stuff and the undefined variable messages aren't showing for $price and $fullname anymore but they are still not printing. I tried using echo too but that doesn't work either.

 

the only actual error i'm getting is the result of a query i commented off.

Notice: Undefined variable: result in /web/rl1426/8/transaction_added.php on line 65 Warning: mysql_free_result() expects parameter 1 to be resource, null given in /web/rl1426/8/transaction_added.php on line 65

 

but the issue is that $fullname is printing out as "Array" and $price is printing out as nothing. I don't know what errors you want me to post up.

 

<html>
<head>
<link href="transaction.css" rel="stylesheet" type="text/css" />
<title>Transaction Added</title>
</head>

<?php

include ('/home/rl1426/config.php');
$db_link = mysql_connect($db_server, $db_user, $db_password);
if (!$db_link)
    die("Could not connect: " . mysql_error());
print( nl2br("<small><small><small>Connected successfully</small></small></small>\n"));
$db_selected = mysql_select_db($db_name, $db_link);
if (!$db_selected) 
    die("Can't use \"$db_name\" : " . mysql_error());
print( nl2br("<small><small><small>Selected successfully</small></small></small>\n"));

/* setting variables from HTML */
$the_date  = $_POST['the_date'];
$ticker    = $_POST['ticker']; 
$shares    = $_POST['shares'];
$client_ID = $_POST['client_ID'];
$buy       = $_POST['buy'];
$dataCheck = 1;

$trans_ID = "NULL";

if (empty ($shares))
   {
   print("<h2>Please input how many shares of stock you would like to buy/sell.</h2>\n");
   $dataCheck = 0;
   }

if ($dataCheck > 0) 
{

/* Performing SQL SELECT price query*/
$query1 = "SELECT close FROM stocks WHERE the_date = '$the_date' AND ticker = '$ticker' ";
$result1 = mysql_query($query1) or die("Query failed : " . mysql_error()); 
$price = mysql_fetch_assoc($result1);

$query2 = "SELECT CONCAT(fname,' ',lname)FROM clients WHERE client_ID = '$client_ID' ";
$result2 = mysql_query($query2) or die("Query failed : " . mysql_error()); 
$fullname = mysql_fetch_assoc($result2);

/* Printing results in HTML */
print("<h1>Transaction Has Been Added:<br></h1><hr />\n");
print("<h2>\t Client Name: $fullname <br>\n");
print("\t Date of Transaction: $the_date <br>\n ");
print("\t Stock Ticker: $ticker <br>\n");
print("\t Number of Shares: $shares <br>\n");
print("\t Price of Stock: $price <br>\n");
}
else {
 print("<p><h2>Error. Please try again.</p>\n</h2>");
 }

 /* Performing SQL INSERT query 
$query4 = "INSERT INTO transactions (trans_ID,the_date,ticker,shares,price,client_ID,buy)
		VALUES (\"$trans_ID\",$the_date,\"$ticker\",$shares,$price,$client_ID,$buy)";
$result = mysql_query($query4) or die("Query failed : " . mysql_error()); */

  /* Free resultset */
  mysql_free_result($result);

  /* Closing connection */
  mysql_close($db_link);

?>
</body>
</html>	
Link to comment
Share on other sites

this is the output on the page:

Connected successfully

Selected successfully

Transaction Has Been Added:

 

Client Name: Array

Date of Transaction: 2013-04-10

Stock Ticker: JCP

Number of Shares: 10

Price of Stock:

Notice: Undefined variable: result in /web/rl1426/8/transaction_added.php on line 65 Warning: mysql_free_result() expects parameter 1 to be resource, null given in /web/rl1426/8/transaction_added.php on line 65

 

and this is the view source


<html>
<head>
<link href="transaction.css" rel="stylesheet" type="text/css" />
<title>Transaction Added</title>
</head>


<small><small><small>Connected successfully</small></small></small><br />
<small><small><small>Selected successfully</small></small></small><br />
<h1>Transaction Has Been Added:<br></h1><hr />
<h2>	 Client Name: Array <br>
	 Date of Transaction: 2013-04-10 <br>
 	 Stock Ticker: JCP <br>
	 Number of Shares: 10 <br>
	 Price of Stock:  <br>

Notice: Undefined variable: result in /web/rl1426/8/transaction_added.php on line 65

Warning: mysql_free_result() expects parameter 1 to be resource, null given in /web/rl1426/8/transaction_added.php on line 65
</body>
</html>
	
Edited by iamnothyper
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.