Ruud Hermans Posted August 7, 2009 Share Posted August 7, 2009 I am trying to write a script that allows me to run a mysql query by inserting it into a form. The database is created but when I try to add anything in it there is a error. <html> <head> <title></title> </head> <body> <form method="post"> <table width="80%" border="0" align="center"> <tr> <td width="100">Query</td> <td><textarea name="run" cols="50" rows="10"></textarea></td> </tr> <tr> <td width="100"> </td> <td> </td> </tr> <tr> <td colspan="2" align="center"><input name="save" type="submit" value="Run query"></td> </tr> </table> </form> <?php if(isset($_POST['save'])) { include 'config.php'; include 'opendb.php'; $query = 'CREATE DATABASE survivalist'; $result = mysql_query($query); mysql_select_db('survivalist') or die('Cannot select database'); $run = $_POST['run']; if(!get_magic_quotes_gpc()) { $run = addslashes($run); } $query="$run"; mysql_query($query) or die('Error ,query failed'); include 'library/closedb.php'; echo "Query has been inserted without errors"; } ?> </body> </html> What I presume is that the error is somewhere here sins the message gets displayed. $query="$run"; mysql_query($query) or die('Error ,query failed'); Any help would be more then welcome. Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/ Share on other sites More sharing options...
Zyx Posted August 7, 2009 Share Posted August 7, 2009 First of all, don't copy the query between the variables just to copy them - it's a nonsense, like packing a bag just to repack it immediately to another one . Use mysql_error() to get the error information about the query: if(!get_magic_quotes_gpc()) { $_POST['run'] = addslashes($_POST['run']); } mysql_query($_POST['run']); if(mysql_errno()) { die('An error occured: '.mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892700 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 I am quite new to this. Sorry for asking but what would the full PHP code look like then sins it also needs to create the database first? Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892707 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 It seems like you are creating a database each time you want to insert something new. This is not how a webapp works. You need to create a database before you insert something. When you have created a database you need a table inside your database. Tables inside a database are used to store data. You can use PHP to manipulate data in your database. I suggest you look into the following basic SQL commands: select insert update delete Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892709 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 I have been looking into those and know partly how to work with them. But this script is ment as a try out for inserting MySQL queries from a form. Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892710 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 To be honest I just think that script is plain wrong. the following just should not be in there $query = 'CREATE DATABASE survivalist'; But this script is ment as a try out for inserting MySQL queries from a form. That's why I mentioned the basic SQL commands. You need a insert query such as <?php $sql=" INSERT INTO TABLE table_name ( val1, val2, val3, etc ) VALUES( '{$val1}', '{$val2}', '{$val3}', '{$etc}', )"; mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892711 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 First of all, don't copy the query between the variables just to copy them - it's a nonsense, like packing a bag just to repack it immediately to another one . Use mysql_error() to get the error information about the query: if(!get_magic_quotes_gpc()) { $_POST['run'] = addslashes($_POST['run']); } mysql_query($_POST['run']); if(mysql_errno()) { die('An error occured: '.mysql_error()); } Running into a error: Parse error: parse error in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\try\query.php on line 46 That would be the line with the closing tag for html. Full code now: <html> <head> <title></title> </head> <body> <form method="post"> <table width="80%" border="0" align="center"> <tr> <td width="100">Query</td> <td><textarea name="run" cols="50" rows="10"></textarea></td> </tr> <tr> <td width="100"> </td> <td> </td> </tr> <tr> <td colspan="2" align="center"><input name="save" type="submit" value="Run query"></td> </tr> </table> </form> <?php if(isset($_POST['save'])) { include 'config.php'; include 'opendb.php'; $query = 'CREATE DATABASE survivalist'; $result = mysql_query($query); mysql_select_db('survivalist') or die('Cannot select database'); $run = $_POST['run']; if(!get_magic_quotes_gpc()) { $_POST['run'] = addslashes($_POST['run']); } mysql_query($_POST['run']); if(mysql_errno()) { die('An error occured: '.mysql_error()); } ?> </body> </html> Any solutions? Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892712 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 To be honest I just think that script is plain wrong. I am pretty sure it is but I am just trying a few things to see if they would work so I understand the language better. Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892713 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 I was more thinking of something like this <?php if(isset($_POST['save'])) { include 'config.php'; // probably your db connect vars such as host dbname user and password include 'opendb.php'; // Don't know what's in here is this your database connection script? $run = mysql_real_escape_string ($_POST['run']); // sql injection prevention $query =" INSERT INTO TABLE survivalist ( run ) VALUES( '{$run}', )"; mysql_query($query) or die(mysql_error()); } ?> <html> <head> <title></title> </head> <body> <form method="post"> <table width="80%" border="0" align="center"> <tr> <td width="100">Query</td> <td><textarea name="run" cols="50" rows="10"></textarea></td> </tr> <tr> <td width="100"> </td> <td> </td> </tr> <tr> <td colspan="2" align="center"><input name="save" type="submit" value="Run query"></td> </tr> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892719 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 I was more thinking of something like this But then I am inserting something into a table. The meaning is to insert into a database so I can create a table in the form. Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892722 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 Ugh re-reading the comments you made I think I just missunderstood. I'm grabbing another coffee. You're simply trying to execute sql commands using a form. Similar to running a query on a command line. Just don't use it on any public server that would be just too risky <?php if(isset($_POST['save'])) { include 'config.php'; include 'opendb.php'; $query = stripslashess ($_POST['run']); mysql_query($query) or die(mysql_error()); } ?> edit Hmmm or did you mean you want to fill in a table name to create a new table? Now I am not sure what you mean could you elaborate a little more? Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892725 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 Hmmm or did you mean you want to fill in a table name to create a new table? Now I am not sure what you mean could you elaborate a little more? Hahaha, I would like to write this entire query plus the creating of the table into a textfield and then run it. 'CREATE TABLE news( '. 'cid INT NOT NULL AUTO_INCREMENT, '. 'title VARCHAR(20) NOT NULL, '. 'content VARCHAR(50) NOT NULL, '. 'PRIMARY KEY(cid))'; Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892726 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 Hahaha, I would like to write this entire query plus the creating of the table into a textfield and then run it. CREATE TABLE news( cid INT NOT NULL AUTO_INCREMENT, title VARCHAR(20) NOT NULL, content VARCHAR(50) NOT NULL, PRIMARY KEY(cid) ); Do you mean you just want to enter that query and it should be executed? It is indeed a query that will create a new table Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892727 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 Hahaha, I would like to write this entire query plus the creating of the table into a textfield and then run it. CREATE TABLE news( cid INT NOT NULL AUTO_INCREMENT, title VARCHAR(20) NOT NULL, content VARCHAR(50) NOT NULL, PRIMARY KEY(cid) ); Do you mean you just want to enter that query and it should be executed? It is indeed a query that will create a new table Yes. Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892729 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 Then the previous code should work I guess haven't tested it so it might produce errors <?php if(isset($_POST['save'])) { include 'config.php'; include 'opendb.php'; $query = stripslashes ($_POST['run']); mysql_query($query) or die(mysql_error()); } ?> <html> <head> <title></title> </head> <body> <form method="post"> <table width="80%" border="0" align="center"> <tr> <td width="100">Query</td> <td><textarea name="run" cols="50" rows="10"></textarea></td> </tr> <tr> <td width="100"> </td> <td> </td> </tr> <tr> <td colspan="2" align="center"><input name="save" type="submit" value="Run query"></td> </tr> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892730 Share on other sites More sharing options...
Zyx Posted August 7, 2009 Share Posted August 7, 2009 Ruud Hermans -> in my code you have inserted you forgot to close the curly bracket from the first if. Count the curly brackets and you'll see it . Quote Link to comment https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892928 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.