dongsi4547 2013-11-03 15:19
浏览 49
已采纳

我的PHP代码不会让我插入到我的mysql表中

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

  • 写回答

2条回答 默认 最新

  • duanke8011 2013-11-03 15:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式