Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-LocalHost数据库简单连接SQL_Php_Mysql_Sql_Apache_Htdocs - Fatal编程技术网

PHP-LocalHost数据库简单连接SQL

PHP-LocalHost数据库简单连接SQL,php,mysql,sql,apache,htdocs,Php,Mysql,Sql,Apache,Htdocs,我不明白为什么这么简单的事情就这么难 现在,当我点击submit时,我得到一个错误: 注意:第6行C:\xampp\htdocs\DataHandling.php中的未定义变量:conn 致命错误:在第6行的C:\xampp\htdocs\DataHandling.php中调用null上的成员函数prepare() 我的表格有效,代码: <html> <head> <title>Gym Form</title> <meta c

我不明白为什么这么简单的事情就这么难

现在,当我点击submit时,我得到一个错误:

注意:第6行C:\xampp\htdocs\DataHandling.php中的未定义变量:conn

致命错误:在第6行的C:\xampp\htdocs\DataHandling.php中调用null上的成员函数prepare()

我的表格有效,代码:

<html>
<head>
    <title>Gym Form</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="DataHandling.php" method="post">

    <span>Gym Membership Registration</span><br><br>
    <Span>Title: </Span><input type ="text" Value =" " name ="Title" /><br>
    <Span>First Name: </Span><input type ="text" Value =" " name ="Fname" /><br>
    <Span>Last Name: </Span><input type ="text" Value =" " name ="Lname" /><br><br>
    <Span>Gender: </Span><select name ="Gender">
        <option value ="Junior">Male</option>
        <option value ="Adult">Female</option>
        <option value ="Senior">Private</option>
    </select><br>

    <Span>DOB: </Span><input type ="date" name ="DOB" /><br><br>
    <Span>MembershipExpiry: </Span> <input type ="date" name ="MemX" /><br>
    <Span>MembershipType: </Span><select name = "MemType">
        <option value ="Junior">Junior</option>
        <option value ="Adult">Adult</option>
        <option value ="Senior">Senior</option>
    </select><br><br>
    <Span>Email Address: </Span><input type ="email" name ="Email" /><br><br>

    <input type="Submit" name="submit" value ="Submit Form">

体操形式
健身房会员注册

标题:
名字:
姓氏:

性别: 男性 女性 私有的
出生日期:

会员资格到期:
会员资格类型: 年少者 成人 级别高的

电子邮件地址:

然后我收到一条很好的消息,告诉我与数据库的连接已确认,conn.php:

<?php


$hostname = 'localhost';
$username = 'root';
$password = '';
$dbName = 'gym';

try
{
    //Attempt connection passing in predefined connection variables.
    $conn = new PDO("mysql:host=$hostname;dbname=$dbName", $username, $password);
    echo ("Connected to Database Successfully. Welcome ".$username);
}
catch(PDOException $e)
{
    //Use exception E to return PDO/MySQL specific error messages
    echo $sql . "<br>" . $e->getMessage();
}



    </body>
</form>
</html>

?>

然而,我在将表单中输入的数据输入到准备好的数据库时,经历了一段非常糟糕的时间

我试过Sqli,现在又试过别的东西

<?php


    //Prepare HTML insert statement binding parameters
    $stmt = $conn->prepare("INSERT INTO records (Title,Fname,Lname,Gender,DOB,MemX,MemType,Email) 
    VALUES ('$title', '$fname', '$lname', '$gender', '$dob', '$memx', '$memtype', '$email')");

        $stmt ->bindParam(':Title', $title);
        $stmt ->bindParam(':Fname', $fname);
        $stmt ->bindParam(':Lname', $lname);
        $stmt ->bindParam(':Gender', $gender);
        $stmt ->bindParam(':DOB', $dob);
        $stmt ->bindParam(':MemX', $memx);
        $stmt ->bindParam(':MemType', $memtype);
        $stmt ->bindParam(':Email', $email);

    //Attempt row insertion by executing prepared statement
    try
    {
        //Insert a row

        $title = $_POST['Title'];
        $fname = $_POST['Fname'];
        $lname = $_POST['Lname'];
        $gender = $_POST['Gender'];
        $dob = $_POST['DOB'];
        $memx = $_POST['MemX'];
        $memtype = $_POST['MemType'];
        $email = $_POST['Email'];

        $stmt->execute();
    }
    catch (PDOException $e)
    {
        echo $e->getMessage();
    }

    //Close Connection
    $conn = null;

?>

这不是PDO准备好的语句的工作方式。下面是一个例子:

$s = $conn->prepare('SELECT * FROM table WHERE column=:value');
$s->bindParam(':value', $value);

有关详细信息,请参阅。

这不是PDO准备好的报表的工作方式。下面是一个例子:

$s = $conn->prepare('SELECT * FROM table WHERE column=:value');
$s->bindParam(':value', $value);

有关详细信息,请参阅。

如果您在DataHandling.php之上包含conn.php,则由于您正在设置

conn.php末尾的
$conn=null

conn.php

<?php

$hostname = 'localhost';
$username = 'root';
$password = '';
$dbName = 'gym';

$conn = null;
try
{
    //Attempt connection passing in predefined connection variables.
    $conn = new PDO("mysql:host=$hostname;dbname=$dbName", $username, $password);
}
catch(PDOException $e)
{
    //Use exception E to return PDO/MySQL specific error messages
    echo $sql . "<br>" . $e->getMessage();
}

?>

DataHandling.php 你准备好的陈述也是错误的

<?php
require_once 'conn.php';

//Prepare HTML insert statement binding parameters
$stmt = $conn->prepare("INSERT INTO records (Title,Fname,Lname,Gender,DOB,MemX,MemType,Email) 
VALUES (:Title, :Fname, :Lname, :Gender, :DOB, :MemX, :MemType, :Email)");

$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];

//Attempt row insertion by executing prepared statement
try
{
    //Insert a row
    $stmt->bindParam(':Title', $title);
    $stmt->bindParam(':Fname', $fname);
    $stmt->bindParam(':Lname', $lname);
    $stmt->bindParam(':Gender', $gender);
    $stmt->bindParam(':DOB', $dob);
    $stmt->bindParam(':MemX', $memx);
    $stmt->bindParam(':MemType', $memtype);
    $stmt->bindParam(':Email', $email);

    $stmt->execute();
}
catch (PDOException $e)
{
    echo $e->getMessage();
}

//Close Connection
$conn = null;

?>

如果在DataHandling.php之上包含conn.php,则在设置

conn.php末尾的
$conn=null

conn.php

<?php

$hostname = 'localhost';
$username = 'root';
$password = '';
$dbName = 'gym';

$conn = null;
try
{
    //Attempt connection passing in predefined connection variables.
    $conn = new PDO("mysql:host=$hostname;dbname=$dbName", $username, $password);
}
catch(PDOException $e)
{
    //Use exception E to return PDO/MySQL specific error messages
    echo $sql . "<br>" . $e->getMessage();
}

?>

DataHandling.php 你准备好的陈述也是错误的

<?php
require_once 'conn.php';

//Prepare HTML insert statement binding parameters
$stmt = $conn->prepare("INSERT INTO records (Title,Fname,Lname,Gender,DOB,MemX,MemType,Email) 
VALUES (:Title, :Fname, :Lname, :Gender, :DOB, :MemX, :MemType, :Email)");

$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];

//Attempt row insertion by executing prepared statement
try
{
    //Insert a row
    $stmt->bindParam(':Title', $title);
    $stmt->bindParam(':Fname', $fname);
    $stmt->bindParam(':Lname', $lname);
    $stmt->bindParam(':Gender', $gender);
    $stmt->bindParam(':DOB', $dob);
    $stmt->bindParam(':MemX', $memx);
    $stmt->bindParam(':MemType', $memtype);
    $stmt->bindParam(':Email', $email);

    $stmt->execute();
}
catch (PDOException $e)
{
    echo $e->getMessage();
}

//Close Connection
$conn = null;

?>


标签在哪里?您需要像注释一样的表单above@Fil有一个问题,OP没有正确格式化问题删除这行
$conn=nullA)这不是必需的。B) 如果它处于这个位置,那么当你尝试在后面的代码中使用它时,你将没有
$conn
对象。现在我看到一个
,但它没有结束标记
这并不难,但你必须注意一些基本要求,以使它正常工作。
标签?你需要把你的表格和评论一样above@Fil有一个问题,OP没有正确格式化问题删除这行
$conn=nullA)这不是必需的。B) 如果它处于这种位置,那么当你尝试在后面的代码中使用它时,你将没有
$conn
对象现在我看到一个
,但它没有结束标记
这并不困难,但你必须注意一些基本要求,以使它正常工作。我对此进行了尝试感谢您提供的信息,但是我收到一个错误消息,说第6行是错误的:@JSint Put
require_once'conn.php'
位于DataHandling.php之上。并删除
$conn=null在conn.php的末尾,没有理由这个答案让我如此高兴。必须是配置文件图片;)@Jek这就是在这个网站上阅读一些问题时的表情:)啊哈,是的,我完全同意@droti尝试了一下,感谢你提供的信息,但是我得到一个错误,说第6行是错误的:@JSint Put
require_once'conn.php'
位于DataHandling.php之上。并删除
$conn=null在conn.php的末尾,没有理由这个答案让我如此高兴。必须是配置文件图片;)@这就是我在这个网站上读到一些问题时的表情:)啊哈,是的,我完全同意