VAR no passing PHP

by 8 replies
9
Hey All I'm back again and can't figure this out...

k, so I have this script I'll post below it basically is linked from another page with grabbing the ID from the URL - when I first bring up this page I'm able to echo out the ID then within this (all code on same page) there is a form that actions itself so you would think the ID would still be here but when I try to run the INSERT $client_id it's empty and I'm getting this error when hitting the submit button for the new note to be added.

DATEUSERMESSAGEACTIONSorry Mate Couldn't select datbaseYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
now the thing is if you see the source code this error is refering to the query that works fine for spitting out the messages prior to hitting the add new note.. also looking at code in the RED message I added the VAR's to see what was coming out and nothing here.. so I'm lost!

here the whole source

PHP Code:
<?php
include ("connect.php");
$id $_GET['id'];
 
if (isset(
$_POST['submit'])) {
$message $_POST['message'];
$date date("F j, Y");
$client_id $_GET['id'];
echo 
$id;
echo 
$client_id;
$insert mysql_query("INSERT INTO `notes` VALUES ('$client_id','$date','$user','$message','')") or die (mysql_error());
 if (
$insert) {
 echo 
"<div align='center'><font color='red'>Note Added Successfully Please Refresh" .$client_id $id "</font></div>";
 }
}
//Client Notes - Should pull up notes related to client selected by ID
?>
<table align="center" border="1">
                        <tr>
                            <th>DATE</th>
                            <th>USER</th>
                            <th>MESSAGE</th>
                            <th>ACTION</th>
                        </tr>
<?php
$query 
mysql_query("SELECT * FROM `notes` WHERE client_id=$id") or die("Sorry Mate Couldn't select datbase" mysql_error());
  while (
$row=mysql_fetch_array($query)) {
   
$num_row mysql_num_rows($query);
  if (
$num_row 0) {
  echo 
"<tr><td>" $row['date'] . "</td><td>" $row['user'] . "</td><td>" $row['message'] . "</td><td><a href='markread.php'><div align='center'><img border='0' src='images/checkmark.gif' width='45' height='45'></a></div></td></tr>";
  }
  else {
  echo 
"No Messages To Produce";
  }
  }
 echo 
"</table>";
 
?>
<div align="center">
<form action="client_notes.php?client_id=<?php echo $id?>" method="POST">
<fieldset id="Add_Notes">
    <legend>ADD NOTES</legend>
    <label for="message">Message:</label> 
    <textarea cols="30" rows="5" name="message"> 
</textarea>
<input id="button1" type="submit" name="submit" value="ADD" /> 
</form>
</div>
#programming #passing #php #var
  • Hi,
    Try replace in your code, to set
  • ok so I pretty much figured out something as to why I'm getting error if I go directly to the page using just the filename client_notes.php i get error (I'm assuming cause the select clause says SELECT * FROM .... WHERE client_id=id or die... so when I go to clients_notes.php?id=22 it pulls up fine..

    I'm loosing the VAR when clicking on add in the form.. I noticed in my above code I did mess up on the GET and POST that was corrected but still not getting VAR to stick.. now sessions I don't believe would work as this script is running within itself
  • You should pass id in your form too, either as separate input (then it will be in $_POST), or in the action URL as CoursesWeb said - then it will be in $_GET.
  • Here something else I just noticed so I'm at client_notes.php?id=22 this screen shows a list of notes assoc to that ID under that is a form to add a new note with a button to submit that note... looking at the code above you see I point the action in the form to client_notes.php? and append the proper php to attached the ID - now when I hover over this button it clearly shows the right url with the right ID when you click it this is what I get client_notes.php?message=+%0D%0Aadfds&submit=ADD so It's dropping the id= why?
  • ok great but if you look at the above code I'm passing it in the action url
  • and yes I caught the fact that I was using POST and have changed to GET and still having this issue so don't post saying change to GET cause it is
  • We don't see what's in connect.php; is it possible that something there is clobbering $_GET['id']? What if you use 'cid' element instead?

    Also, a note of caution, you should always declare the column names in the INSERT statement that the VALUES correspond to. There may come a day when you need to export the DB and the columns when imported may not line up with your values.
  • <form action="client_notes.php?client_id=<?php echo $id; ?>" method="POST"> - your id will be in $_GET['client_id'], not in $_GET['id'] .

Next Topics on Trending Feed

  • 9

    Hey All I'm back again and can't figure this out... k, so I have this script I'll post below it basically is linked from another page with grabbing the ID from the URL - when I first bring up this page I'm able to echo out the ID then within this (all code on same page) there is a form that actions itself so you would think the ID would still be here but when I try to run the INSERT $client_id it's empty and I'm getting this error when hitting the submit button for the new note to be added.