0

I tried everything I could think of, I look at it over and over. I added echo (); to see if the problem was something else. Everything is spelled like it is on the server. I don't know what else to do. Please help!!

<?php
include('../lock.php');

$username    =    $login_session;  
$characterName    =    mysql_real_escape_string($_POST['characterName']);  
$gender =    mysql_real_escape_string($_POST['gender']);   
$height = "58" + rand(2, 20);   
$weight = "120" * rand(2, 8);

echo ($id . "<br />");    
echo ($username . "<br />");  
echo ($characterName . "<br />");  
echo ($gender . "<br />");  
echo ($height . "<br />");   
echo ($weight . "<br />"); 

if(isset($_POST['nc1'])) 

{  
$query    =    "INSERT INTO character(id, username, characterName, gender, height,   weight) VALUES ('$id', '$username', '$characterName', '$gender', '$height', '$weight')";  
mysql_query($query) or die('Error: ' . mysql_error());  
//header('location:success.php');   
echo ("success");  
}  
?>

Jan 29, 2014 - I have made the updates you have suggested(but mysqli) and everything is echoing out correctly, the mysql_error() is returning blank but there is an error. POST is working. I still don't know how to fix it. The connection is also correct.

I take that back, the error is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character (id, username, characterName, gender, height' at line 1

Andrew
  • 77
  • 1
  • 9
  • add mysql_error() and post the message error, ```mysql_query($query) or die(mysql_error()); ``` – rray Nov 03 '13 at 15:23
  • Try `if (!mysql_query($query)) die('Error: ' . mysql_error());` to see if there's an error in your SQL; also, you should try switching to [`mysqli`](http://php.net/manual/en/book.mysqli.php) (instead of the `mysql_` functions) and use prepared statements. – newfurniturey Nov 03 '13 at 15:23
  • what is actually happening? **do you get an error?** is it the page actually receiving the `$_POST` information (or is it blank)? what happens if you dump the query? **will that query run on its own** (outside of PHP, directly in mysql) or does it get errors? is your connection to the database set up properly? (also, note that `mysql_` functions are old and open you up to [sql injection](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). you should be using `mysqli_` or `pdo` with prepared statements or binding) – gloomy.penguin Nov 03 '13 at 15:33
  • I second what @ShankarDamodaran said. If your query's coming from a POST, then it should be in the form of `$id = $_POST['id'];` – Funk Forty Niner Nov 03 '13 at 16:04

2 Answers2

1

First of all, you must specify the logical structure of the database in your question, and it seems that your id is a primary key and you have done $id = $id. This may cause the id to be duplicate or null so the first check for that variable is set or not and also check for other constraints like not null. If it is applied in the fields and is not fulfilled the rows can't be inserted too.

Secondly check your database connection string, if you are reachable to database through the connection string like,

boolean function connect($host, $uname , $pass){
if(mysql_connect($host, $uname, $pass))
return true;
else 
return false;
}

just try this may you be able to insert rows

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
dralmostright
  • 61
  • 1
  • 3
0

Make sure your sql_connect is correct , if you are using a localhost then you should have something like

mysql_connect("localhost","root","password") or die(mysql_error)

make sure to use or die because this would show any error that you may have with your database connection

Diego Claudiu
  • 332
  • 1
  • 9