开发者

Simple php/mysql not working

I have the following in a php script.All I get is a blank page, no errors or nothing.

error_reporting(E_ALL);
ini_set("display_errors", 1);
$database = "mydatabase";
$con = mysql_connect("localhost", "admin", "password") or die(mysq开发者_如何学Cl_error());
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db = mysql_select_db($database);
if(!$db){
    die('Could not connect: ' . mysql_error());
}
if(isset($_POST['id'])){
$userid = mysql_real_escape_string($_POST['id']);
echo($userid);  
}
if(isset($_POST['name')){
$username = mysql_real_escape_string(htmlentities($_POST['name'])); 
echo($username);
}

$query = mysql_query("SELECT * FROM userinfo  
WHERE userid ='$userid'")or die(mysql_error()); 
if(mysql_num_rows($query) > 0){
    echo "yeah";
}else{
$query = mysql_query("INSERT INTO userinfo (username,userid)
VALUES ($username,$userid)")or die(mysql_error());
if(mysql_affected_rows($query)== 1){
echo "UPDATED";

}else{
    echo "NOPE";
}
}


You should format your code better. Also you where missing a close ] bracket on this line, if (isset($_POST['Name')) {

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$database = "mydatabase";
$con = mysql_connect("localhost", "admin", "password") or die(mysql_error());

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

$db = mysql_select_db($database);
if(!$db)
{
    die('Could not connect: ' . mysql_error());
}

if(isset($_POST['id']))
{
    $userid = mysql_real_escape_string($_POST['id']);
    echo($userid);  
}

if(isset($_POST['name']))
{
    $username = mysql_real_escape_string(htmlentities($_POST['name'])); 
    echo($username);
}

$query = mysql_query("SELECT * FROM userinfo WHERE userid ='$userid'")or die(mysql_error()); 
if(mysql_num_rows($query) > 0)
{
    echo "yeah";
}
else
{
    $query = mysql_query("INSERT INTO userinfo (username,userid) VALUES ($username,$userid)")or die(mysql_error());
    if(mysql_affected_rows($query)== 1)
    {
        echo "UPDATED";
    }
    else
    {
        echo "NOPE";
    }
}
?>


You also have an error in your SQL:

INSERT INTO userinfo (username,userid)
VALUES ($username,$userid)

The values here should be quoted:

INSERT INTO userinfo (username,userid)
VALUES ('$username', '$userid')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜