insert statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

insert statement

Post by twb »

Hi, If someone could help me it would be appreciated,
I have a form that has 4 textboxes anda function that takes the input from these and adds it to a mysql db. My problem is my INSERT statement dosen't seem to work:
The code is as follows

Code: Select all

function Add_new_details($task_name,$task_description,$task_date,$task_time){ 
$query = "INSERT INTO calander (task_name, task_description, task_date, task_time) "; 
$query .= "VALUES ('$task_name', '$task_description','$task_date','$task_time') "; 
    echo $query;//this prints out a empty set for the VALUES 
    $result = mysql_query($query); 
    echo $result; //this prints out "1" 
    if(!$result){ 
        echo "Error in search for results:".mysql_error(); 
        //exit(); 
        } 
}
The function call is as follows:

Code: Select all

Add_new_details($task_name,$task_description,$task_date,$task_time);
The function when called dosen't recieve the values of the variables...I'M not sure why?
I can manually insert into the db, so I know it can be done.
Thank you for your time
twb
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

try echo-ing out the query and seeing what its actually doing
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how are the varibles set up that are being passed into this function? that's where the error probably lies.
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

insert statement

Post by twb »

Yes I did do that already and it returns empty values. I also echoed out the variables in the function call and they also have empty values, so I gather thats where my problem is. I declared the variables as follows
//get variables

Code: Select all

$task_name = isset($_POSTї'task_name'])? $_POSTї'task_name'] : "";
$task_date = isset($_POSTї'task_date'])? $_POSTї'task_date'] : "";
$task_time = isset($_POSTї'task_time']) ? $_POSTї'task_time'] : "";
$task_description = isset($_POSTї'task_description']) ? $_POSTї'task_description'] : "";
I'm not sure why the variables arn't being set from the text boxes
thanks
twb
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

var_dump($_POST) and see what that tells you
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you wrote //get variables.. yet you use $_POST, are you sure they are being posted?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

i think he meant like "Get the variables" not "these are the GET variables"
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

Insert Statement

Post by twb »

I am sure that is where my problem is, that these variables arn't being POSTED, that what I can't figure out why. Althought I am new to this I can't see the difference to what Iv'e done in the past.
Thanks twb
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the html form that submits to this script/codepath?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

if that was your inset statement in that other thread:

Code: Select all

tr><td>Date:
      <input name="task_date" type="text" size="10"></td></tr>
    <tr><td>Time:
       <input type="text" name="task_time" size="20"></td></tr>
   <tr><td>Task Name:
      <input type="text" name="task_name" size="50"> </td></tr>
   <tr></tr><td>Description:
      <input type="text" name="task_description" size="50"></td></tr>
you have no <form> tags - and if you just left them off, that would be the part we need to see and if you actually don't have them, that's your problem
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

insert statement

Post by twb »

magicrobotmonkey
sorry for the confusion..
the full html is as follows

Code: Select all

<body>
<table width="800" border="1">
	<tr><td align="center"><p><strong> Calander Details:</strong></p>
	</tr>
 	<tr><td>Date:
 	  <input name="task_date" type="text" size="10"></td></tr>
 	<tr><td>Time:
    	<input type="text" name="task_time" size="20"></td></tr>
	<tr><td>Task Name:
		<input type="text" name="task_name" size="50"> </td></tr>
	<tr></tr><td>Description:
		<input type="text" name="task_description" size="50"></td></tr>
	<td><table border="1" width="261">
			<form name="form1" method="post" action="">
			<tr>
				<td width="100"><input type="submit" name="Submit" value="Submit"></td>
				<td width="100"><input type="submit" name="Submit" value="null"></td>
				<td width="50"><input type="hidden" name="action" id="action" value="CHECK"></td>
			</form>
			</tr>
	</table></td>
</table>
</body>
the function call above this is as follows:
//Get ACTION Variable

Code: Select all

$action = isset($_POST&#1111;'action']) ? $_POST&#1111;'action'] : '';

Code: Select all

//If action equal 'CHECK' then the user has submitted data to checking
if (strtoupper($action) == 'CHECK') &#123;
  //function call to insert into the db
Add_new_details($task_name,$task_description,$task_date,$task_time);

&#125;//CLOSE IF
thanks
twb
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

right you have no <form> tags on your page. You need some.

http://www.w3.org/TR/REC-html40/interact/forms.html
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

insert statement

Post by twb »

hmmmm I thought that

Code: Select all

<form name="form1" method="post" action=""> 
         <tr> 
            <td width="100"><input type="submit" name="Submit" value="Submit"></td> 
            <td width="100"><input type="submit" name="Submit" value="null"></td> 
            <td width="50"><input type="hidden" name="action" id="action" value="CHECK"></td> 
         </form>
was my form tag... however I will read up on the link you supplied,
thanks
twb
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

oh i didnt even see that there. d'oh. the problm is that all inputs need to be within the form tag and i think you are going to need something in action, but I'm not sure
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

insert statement

Post by twb »

magicrobotmonkey
THATS IT!!!
I moved the form tag to include the the input and it works like a charm
Thanks for your help and patients
TWB
Post Reply