简体   繁体   中英

Unknow php error in sql INSERT INTO

I have problem in my registration. Look on my code:

//player.php
<?php
session_start();
class Player
{
var $name;
function _construct($name)
{
    $this->$name = $name;
}
function CreatePlayer($name, $pass, $mail, $date, $type)
{
    if($_POST['submit'])
    {
        $link = mysql_connect("localhost","wewewe", "wewewe");
        if(!$con)
        {
            die( $return = mysql_error());
        }   
        mysql_select_db("wewewe", $con);
        mysql_query("INSERT INTO USERS (name, pass, mail, date, type) VALUES ('$name', '$pass', '$mail', '$date','$type')");
        mysql_close(link);
    }
    return $return;
}
function LoginPlayer($name, $pass)
{

    $link = mysql_connect("localhost","username", "pass");
    mysql_select_db("con", $con);
    $result = mysql_query("SELECT FROM USERS WHERE pass='$pass' AND name='$name'");
    $count = mysql_num_rows($result);
    if($count==1)
    {
        $_SESSION['name'] = $name;
        $_SESSION['logged'] = true;
    }
    mysql_close($link);
}
}
?>
//reg.php
<html>
<form action="log.php" method="post">
Meno: <input type="text" name="meno">
<br>
Heslo: <input type="text" name="heslo">
<br>
Mail: <input type="text" name="mail">
<br>
Date: <input type="text" name="date">
<br>
Type: <input type="text" name="type">
<br>
<input type="submit">
<br>
</form>
<?php

include 'player.php';
$name = $_POST['meno'];
$pass = $_POST['heslo'];
$mail = $_POST['mail'];
$date = $_POST['date'];
$type = $_POST['type'];
$obj = new Player($name);

$res = $obj->CreatePlayer($name, $pass, $mail, $date, $type);
if($res==true)
{
echo "jo!";
}
else
{
echo $res;
}
?>

My problem is that if I write text to all fields and press ok, my page will restart but without any error message. And when I will look to a database, theres nothing. Why? Can anybody plese help me?

EDIT:

 <?php
 session_start();
 class Player
 {
var $name;
function _construct($name)
{
    $this->$name = $name;
}
function CreatePlayer($name, $pass, $mail, $date, $type)
{
    if($_POST['submit'])
    {
        $con = mysql_connect("localhost","wewewe", "wewewe");
        if(!$con)
        {
            die( $return = mysql_error());
        }   
        mysql_select_db("wewewe", $con);
        $query = "INSERT INTO USERS (name, pass, mail, date, type) VALUES ('$name', '$pass', '$mail', '$date','$type')";

        $retrn = var_dump($query); // SHOWS YOU QUERY STRING

        mysql_query($query) or die(mysql_error()); // EXECUTES QUERY OR THROWS EXCEPTON (SHOWS ERROR TOO)
        mysql_close($con);
    }
    return $return;
}
function LoginPlayer($name, $pass)
{

    $link = mysql_connect("localhost","username", "pass");
    mysql_select_db("con", $con);
    $result = mysql_query("SELECT FROM USERS WHERE pass='$pass' AND name='$name'");
    $count = mysql_num_rows($result);
    if($count==1)
    {
        $_SESSION['name'] = $name;
        $_SESSION['logged'] = true;
    }
    mysql_close($link);
}
 }
 ?>

I think its not going inside if($_POST['submit'])

change

<input type="submit">

to

<input name="submit" type="submit" />

<input type="submit"> modify to <input type="submit" name="submit">

To debug SQL INSERTING :

mysql_select_db("wewewe", $con);
mysql_query("INSERT INTO USERS (name, pass, mail, date, type) VALUES ('$name', '$pass', '$mail', '$date','$type')");
mysql_close(link);

modify to

mysql_select_db("wewewe", $con);
$query = "INSERT INTO USERS (name, pass, mail, date, type) VALUES ('$name', '$pass', '$mail', '$date','$type')";

var_dump($query); // SHOWS YOU QUERY STRING

mysql_query($query) or die(mysql_error()); // EXECUTES QUERY OR THROWS EXCEPTON (SHOWS ERROR TOO)
mysql_close(link);

Your query in the function LoginPlayer is incorrect. You may simply fix it like this

$result = mysql_query("SELECT name FROM USERS WHERE pass='$pass' AND name='$name'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM