Jump to content

Query is empty?!


cpharry

Recommended Posts

Hey everyone,

 

I have a form that goes to a processing page, there is a drop down menu on the form that gets locations from a database. The code for the form page is first and then process page.

 

<?
// Connect database
mysql_connect("localhost","USERNAME TAKEN OUT","PASSWORD TAKEN OUT");
mysql_select_db("wowbasec_class"); 

// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
if(isset($select)&&$select!=""){
$select=$_GET['select'];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<table width="70" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#00FFFF">
  <tr>
    <td height="34"><div align="center">Character Add Page </div></td>
  </tr>
  <tr>
    <td height="208"><form action="addcharfile.php" method="post" name="form1">
      <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="191"><div align="right">Name:</div></td>
          <td width="309"><input name="name" type="text" id="name"></td>
        </tr>
        <tr>
          <td><div align="right">Location:</div></td>
          <td><select name="location" id="location">
<option value="">--- Select ---</option>
<?
// Get records from database (table "name_list").
$list=mysql_query("select * from location order by id asc");

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['name']; ?></option>
</select>
</td>
        </tr>
        <tr>
          <td><div align="right">Health:</div></td>
          <td><input name="health" type="text" id="health"></td>
        </tr>
        <tr>
          <td><div align="right">Mana:</div></td>
          <td><input name="mana" type="text" id="mana"></td>
        </tr>
        <tr>
          <td><div align="right">Notes:</div></td>
          <td><textarea name="notes" id="notes"></textarea></td>
        </tr>
        <tr>
          <td colspan="2"><div align="center">
            <input type="submit" name="Submit" value="Add">
          </div></td>
        </tr>
      </table>
    </form>
    </td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>
</body>
</html>
<?
// End while loop.
}
?>

 

<?

$connection = mysql_connect("localhost","USERNAME TAKEN OUT","PASSWORD TAKEN OUT");

mysql_select_db("wowbasec_class", $connection);

// Get values from form.
$name=$_POST['name'];
$location=$_POST['location'];
$health=$_POST['health']; 
$mana=$_POST['mana']; 
$notes=$_POST['notes']; 

// Insert all parameters into database.
// The id field is auto increment. You don't have to insert any value 
$sql = mysql_query("insert into char(name, location, health, mana, notes) values('$name', '$location', '$health', '$mana', '$notes')");

if (!mysql_query($sql,$connection))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

// Close database connection
mysql_close(); 

?>

 

Any help is apreciated,

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/155556-query-is-empty/
Share on other sites

It means that your SQL statement is messed up somewhere... It may have to do with the information that you are passing... You can try this:

 

Change from this:

values('$name', '$location', '$health', '$mana', '$notes')

 

to this:

values(\"$name\",\"$location\",\"$health\",\"$mana\",\"$notes\")

 

and make sure you use the addslashes function on the variables you are passing to the query:

http://us.php.net/addslashes

 

Hope this helps!

Link to comment
https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-822182
Share on other sites

Oh come on!

$sql = mysql_query("insert into char(name, location, health, mana, notes) values('$name', '$location', '$health', '$mana', '$notes')");

if (!mysql_query($sql,$connection))

 

You're doing the query twice. Try:

 

$sql = "INSERT INTO char (name, location, health, mana, notes) VALUES ('$name', '$location', '$health', '$mana', '$notes')";

if (!mysql_query($sql,$connection))

Link to comment
https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-822192
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.