V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
githere
V2EX  ›  问与答

初学 PHP ,请问如何解决 Undefined variable 和 mysqli_error() expects parameter 1 错误

  •  
  •   githere · 2019-07-09 01:00:42 +08:00 · 1353 次点击
    这是一个创建于 1725 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本地环境:macos+xampp+php7


    创建了 2 个文件 demo.php and demo-form.php ,放在 xampp/htdocs/simpleform 目录下

    demo.php 的代码为:

    
    <?php  
    /**
    
    * Created by PhpStorm.
    * User: sulayman
    * Date: 2019/7/8
    * Time: 8:37 PM  
    */
    
    define('DB_Name', 'form1');  
    define('DB_USER', 'root');  
    define('DB_PASSWORD', '');  
    define('DB_HOST', 'localhost');  
    $link = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD);  
    if(!$link){
    
    
    die('could not connect:'.mysqli_error($link));
    
    
    }
    
    $db_selected = mysqli_select_db($link, DB_Name);  
    if(!$db_selected){
    die('cannot use'.DB_Name.':'.mysqli_error($link));
    }  
    
    //echo "you conneted the database successfully";  
    
    $value = $_POST["input1"];
    
    if (isset($_POST['input1'])) {  
    $value = $_POST['input1'];  
    }
    
    $sql = "INSERT INTO demo(input1) VALUES ('$value')";
    
      
    if (!mysqli_query($link,$sql)){  
    die('error:'.mysqli_error());
    
    }
    
    
    
    mysqli_close($link);
    

    demo-form.php 的代码为:

    
    <form action = "demo.php" method = "post" >
    
       <p> Input 1: </p>
        <input type="text" name="input1">
        <input type="submit" value="submit">
    </form>
    
    

    phpmyadmin 的配置

    https://i.ibb.co/bdHsq9R/2019-07-09-12-43-43.png

    如何解决这 2 个错误:

    Notice: Undefined variable: value in /Applications/XAMPP/xamppfiles/htdocs/simpleform/demo.php on line 36

    Warning: mysqli_error() expects parameter 1 to be mysqli, string given in /Applications/XAMPP/xamppfiles/htdocs/simpleform/demo.php on line 40 error:

    7 条回复    2019-07-10 09:14:14 +08:00
    jellybool
        1
    jellybool  
       2019-07-09 01:37:14 +08:00
    1.把 DB_Name 都改成 DB_NAME
    2.把 die('error:'.mysqli_error()); 改为 die('error:'.mysqli_error($link));

    试试。
    2033009565
        2
    2033009565  
       2019-07-09 09:05:28 +08:00
    $sql = "INSERT INTO demo(input1) VALUES ('$value')";
    变成$sql = "INSERT INTO demo(input1) VALUES ('.$value.')";
    githere
        3
    githere  
    OP
       2019-07-09 15:31:23 +08:00
    @jellybool @2033009565 按照 2 位老铁给的提示,全部修改了,现在数据库中可以插入数据了,刷新 demo 表,进入 input1 field,可以看到写入到字段 input1 中的数据,但是还是有 2 个错误:


    Notice: Undefined variable: value in /Applications/XAMPP/xamppfiles/htdocs/simpleform/demo.php on line 36
    error:Duplicate entry '0-..' for key 'PRIMARY'

    目前 36 行的代码是: $sql = "INSERT INTO demo(input1) VALUES ('.$value.')";
    githere
        4
    githere  
    OP
       2019-07-09 16:45:47 +08:00
    @2033009565 照你提供的这个方法操作,input1 这个 field 里面的数据前后都有 .2 个小圆点.
    2033009565
        5
    2033009565  
       2019-07-10 08:50:29 +08:00
    主键是唯一的
    2033009565
        6
    2033009565  
       2019-07-10 08:51:43 +08:00
    你的 demo.php 的第 36 行有错误,变量没定义
    2033009565
        7
    2033009565  
       2019-07-10 09:14:14 +08:00
    把你的主键设置为自动递增,那两个小原点可以去掉
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3383 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:06 · PVG 08:06 · LAX 17:06 · JFK 20:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.