justineaguas Posted November 8, 2009 Share Posted November 8, 2009 Hello all, I get an error called: Parse error: parse error, expecting `'&'' or `T_VARIABLE' I just studied PHP 5 hours ago. Now I have connected to database with mySQL and all. I am trying to enter a value into the table named 'table1'. I want to insert a text into the database. The text will come from a textarea named "fname". I created a function and this is the codes. Here is the function: function insertValues(`regName`) //error in this line { mysql_query("INSERT INTO `table1` (id, fname) VALUES ('1', `".regName."`)"); } Here is the code of the form: <form action="" method="post" onclick="<?php insertValues(this); ?>> <td> <input name="regName" type="text" /> </td> <td> <input name="submit" type="submit" value="Register!" /> </td> </form> I'm just a newb. Thanks to those who will help. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 8, 2009 Share Posted November 8, 2009 1. Please insert your code in tags for better readability. 2. function insertValues($regName) //error in this line { mysql_query("INSERT INTO `table1` (id, fname) VALUES ('1', '$regName')"); } 3. I expect you'll have also problems here. What 'this' is supposed to be? <?php insertValues(this); ?> Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 PHP is parsed serverside (before the content is outputted to the browser) and can't be triggered by client side events like javascript. For this you would need to look into AJAX; although that would probably be far too advanced for you at the moment. I think what your trying to do is this: <?php // Connect to mysql or include file where connection is established... function insertValues($regName) { mysql_query("INSERT INTO `table1` (id, fname) VALUES (1, '" . $regName . "')"); } if ( isset($_POST['submit']) ) insertValues( mysql_real_escape_string($_POST['regName']) ); ?> <form action="" method="post"> <td> <input name="regName" type="text" /> </td> <td> <input name="submit" type="submit" value="Register!" /> </td> </form> Quote Link to comment Share on other sites More sharing options...
justineaguas Posted November 8, 2009 Author Share Posted November 8, 2009 Thanks to the both of you. There is no error now! However, the value is still not being inserted into the database. @Mchl I tried to replaced 'this' with 'regName'. @Andy-H I tried your code. There were no error but still the values were not inserted. Thanks really. Sorry I'm just new to PHP. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 8, 2009 Share Posted November 8, 2009 @Mchl I tried to replaced 'this' with 'regName'. That will still not work for the reasons outlined by Andy-H. @Andy-H I tried your code. There were no error but still the values were not inserted. Did you connect to the database first? In any case, add some debugging to your query: mysql_query("INSERT INTO `table1` (id, fname) VALUES (1, '" . $regName . "')") or trigger_error(mysql_error(),E_USER_ERROR); Quote Link to comment Share on other sites More sharing options...
justineaguas Posted November 8, 2009 Author Share Posted November 8, 2009 Yes. I have connected to the database. I know how to create databases, tables, etc. with PHP. But I'm having problems integrating HTML codes with PHP codes. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 Is your "id" field a primary key set with auto_increment? mysql_query("INSERT INTO `table1` (id, fname) VALUES (NULL, '" . $regName . "')") or trigger_error(mysql_error(),E_USER_ERROR); Quote Link to comment Share on other sites More sharing options...
justineaguas Posted November 8, 2009 Author Share Posted November 8, 2009 It has an error. "There is no database selected" on this line trigger_error(mysql_error(),E_USER_ERROR); Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 8, 2009 Share Posted November 8, 2009 It has an error. "There is no database selected" on this line trigger_error(mysql_error(),E_USER_ERROR); So... What do you think you need to do? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 mysql_select_db Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.