undefined index error in mysql query

后端 未结 4 602
时光取名叫无心
时光取名叫无心 2020-12-20 10:44


        
相关标签:
4条回答
  • 2020-12-20 11:04

    I think the problem is you didn't entered the if loop,since You do not have your submit button named: Try this instead

    <input type="submit" class="submit1" value="Submit" name="submit"/>
    
    0 讨论(0)
  • 2020-12-20 11:04

    Unless the form is posted, the variables in this will never have a value:

      $query="insert into register(name,phone,email,pass) values ('$name1','$phone1','$email1','$pass1')";
    

    If you need to wait for a POST to execute your query, put that code inside your if:

    $conn = mysql_connect("localhost","root","");
    
    if(!$conn)
        die("cannot connect");
    
    $db = mysql_select_db("ticket",$conn);
    
    if(!$db)
        die("no db");
    
    if(isset($_POST['submit']))
    {
        $name1 = $_POST['name'];
        $phone1 = $_POST['phone'];
        $email1 = $_POST['email'];
        $pass1 = $_POST['password'];
    
        $query = "insert into register(name,phone,email,pass) values ('$name1','$phone1','$email1','$pass1')";
        $result = mysql_query($query);
    
        if(!$result)
            die("Error in pushing".mysql_error());
    }
    
    mysql_close($conn);
    
    0 讨论(0)
  • 2020-12-20 11:06

    Put the mysql query in the if statement curly braces just like this:

    if(isset($_POST['submit'])) {
    $name1=$_POST['name'];
    $phone1=$_POST['phone'];
    $email1=$_POST['email'];
    $pass1=$_POST['password'];
    $query="INSERT INTO `register`(`name`,`phone`,`email`,`pass`) VALUES('$name1','$phone1','$email1','$pass1')";
    $result=mysql_query($query) or die("Error in pushing".mysql_error());
    mysql_close($conn);
    }
    ?>
    

    When the form isn't submited, your variables are not defined and you are querying the db with them. So you put everything in the if statement, so that only when the form is submitted that the query will execute. Also you are vulnerable to mysql injection, use the mysql_real_escape_string function or use mysqli prepared statement.

    0 讨论(0)
  • 2020-12-20 11:22

    You're checking for the presence of $_POST['submit'], and setting some variables if it's found. You're then executing your mysql query outside the conditional block, so if the $_POST variables aren't found you'll get undefined variable errors.

    Try

    <?php
      $conn=mysql_connect("localhost","root","")
      or die("cannot connect");
        $db=mysql_select_db("ticket",$conn)
        or die("no db");
        if (isset($_POST['submit']))
        {
        $name1=$_POST['name'];
        $phone1=$_POST['phone'];
        $email1=$_POST['email'];
        $pass1=$_POST['password'];
        // This code now inside if block
        $query="insert into register(name,phone,email,pass) values ('$name1','$phone1','$email1','$pass1')";
        $result=mysql_query($query)
          or die("Error in pushing".mysql_error());
        mysql_close($conn);
      }
      ?>
    
    0 讨论(0)
提交回复
热议问题