Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/
Share on other sites

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());
}

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892700
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892709
Share on other sites

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());

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892711
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892712
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892719
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892725
Share on other sites

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))';

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892726
Share on other sites

 

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

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892727
Share on other sites

 

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.

Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892729
Share on other sites

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>


Link to comment
https://forums.phpfreaks.com/topic/169190-beginner-help/#findComment-892730
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.