Jump to content

MySQL query doesn't work. [INSERT QUERY]


3raser

Recommended Posts

If you also have any feedback on my code, please do tell me. I wish to improve my coding base.

 

Basically when you fill out the register form, it will check for data, then execute the insert query. But for some reason, the query will NOT insert into the database. In the following code below, I left out the field ID. Doesn't work with it anyways, and I'm not sure it makes a difference.

 

Code:

 

mysql_query("INSERT INTO servers (username, password, name, type, description, ip, votes, beta) VALUES ($username, $password, $name, $server_type, $description, $ip, 0, 1)");

 

Full code:

 

<?php
include_once("includes/config.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><? $title; ?></title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
</head>
<body>

<div id="wrap">

<div id="header">
<h1><? $title; ?></h1>
<h2><? $description; ?></h2>
</div>

<? include_once("includes/navigation.php"); ?>

<div id="content">
<div id="right">
<h2>Create</h2>
<div id="artlicles">
<?php

if(!$_SESSION['user'])
{

        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $name = mysql_real_escape_string($_POST['name']);
        $server_type = mysql_real_escape_string($_POST['type']);
        $description = mysql_real_escape_string($_POST['description']);

        if(!$username || !$password || !$server_type || !$description || !$name)
        {

        echo "Note: Descriptions allow HTML. Any abuse of this will result in an IP and account ban. No warnings!<br/>All forms are required to be filled out.<br><form action='create.php' method='POST'><table><tr><td>Username</td><td><input type='text' name='username'></td></tr><tr><td>Password</td><td><input type='password' name='password'></td></tr>";
        echo "<tr><td>Sever Name</td><td><input type='text' name='name' maxlength='35'></td></tr><tr><td>Type of Server</td><td><select name='type'>
       
        <option value='Any'>Any</option>
        <option value='PvP'>PvP</option>
        <option value='Creative'>Creative</option>
        <option value='Survival'>Survival</option>
        <option value='Roleplay'>RolePlay</option>
       
        </select></td></tr>
       
        <tr><td>Description</td><td><textarea maxlength='1500' rows='18' cols='40' name='description'></textarea></td></tr>";
        echo "<tr><td>Submit</td><td><input type='submit'></td></tr></table></form>";
        }
        elseif(strlen($password) < 
        {
        echo "Password needs to be higher than 8 characters!";
        }
        elseif(strlen($username) > 13)
        {
                echo "Username can't be greater than 13 characters!";
        }
        else
        {
                $check1 = mysql_query("SELECT username,name FROM servers WHERE username = '$username' OR name = '$name' LIMIT 1");
               
                if(mysql_num_rows($check1) < 0)
                {
                        echo "Sorry, there is already an account with this username and/or server name!";
                }
                else
                {
                        $ip = $_SERVER['REMOTE_ADDR'];

                        mysql_query("INSERT INTO servers (username, password, name, type, description, ip, votes, beta) VALUES ($username, $password, $name, $server_type, $description, $ip, 0, 1)");
                        echo "Server has been succesfully created!";
                }
        }
       
}
else
{
        echo "You are currently logged in!";
}

?>
</div>
</div>

<div style="clear: both;"> </div>
</div>

<div id="footer">
<a href="http://www.templatesold.com/" target="_blank">Website Templates</a> by <a href="http://www.free-css-templates.com/" target="_blank">Free CSS Templates</a> - Site Copyright MCTop
</div>
</div>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/229458-mysql-query-doesnt-work-insert-query/
Share on other sites

Justin any error message being displayed?

 

If the field ID is auto then you don't need to include as it'll updated anyhow.

 

At a glance I would try renaming your third column "name" to something else and see if that makes a difference. Not entirely sure if it will but vaguely remember running into issues with naming conventions before.

 

HTH

All strings in a query must be quoted, your's are not, so the query is failing. Since you're not checking the success of the query, you didn't know that.

 

Use:

<?php
   $q = "INSERT INTO servers (username, password, `name`, `type`, description, ip, votes, beta) VALUES ('$username', '$password', '$name', '$server_type', '$description', '$ip', 0, 1)";
   $rs = mysql_query($q);
   if ($rs) {
       echo "Server has been succesfully created!";
   } else {
       echo "There was a problem with the query: $q<br>" . mysql_error();
   }
?>

 

Ken

 

All strings in a query must be quoted, your's are not, so the query is failing. Since you're not checking the success of the query, you didn't know that.

 

Use:

<?php
   $q = "INSERT INTO servers (username, password, `name`, `type`, description, ip, votes, beta) VALUES ('$username', '$password', '$name', '$server_type', '$description', '$ip', 0, 1)";
   $rs = mysql_query($q);
   if ($rs) {
       echo "Server has been succesfully created!";
   } else {
       echo "There was a problem with the query: $q<br>" . mysql_error();
   }
?>

 

Ken

 

Thanks. The error was that type and name needed the `

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.