无法使用PHP将查询连接/处理到Wamp服务器上的MySQL数据库
免责声明是的,数据还没有被验证,是的,它不安全,但这是在本地机器上进行的测试,所以我不担心SQL注入 嗯。所以我有一张用户填写的表格。我在那一页的开头开始一个会话。信息被发布到第二个页面,用户可以在其中验证数据,然后单击按钮提交数据。再次启动会话,并将数据传递到第三个页面,在那里连接到数据库无法使用PHP将查询连接/处理到Wamp服务器上的MySQL数据库,php,mysql,database-connection,wamp,Php,Mysql,Database Connection,Wamp,免责声明是的,数据还没有被验证,是的,它不安全,但这是在本地机器上进行的测试,所以我不担心SQL注入 嗯。所以我有一张用户填写的表格。我在那一页的开头开始一个会话。信息被发布到第二个页面,用户可以在其中验证数据,然后单击按钮提交数据。再次启动会话,并将数据传递到第三个页面,在那里连接到数据库 <?php session_start(); require_once("dbconnect.php"); include("header.php"); echo "This is for a Li
<?php
session_start();
require_once("dbconnect.php");
include("header.php");
echo "This is for a Live Sale: title is " . $_SESSION['title'] . "<p> type is " . $type = $_SESSION['typeofsale'] . "<p> fname is " . $_SESSION['fname'] . " <p>lname is " . $lname = $_SESSION['lname'] . "<p> email is " . $email = $_SESSION['email'] . "<p> address is " . $address = $_SESSION['address'] . " <p>zip is " . $_SESSION['zip'] . " <p>city is " . $_SESSION['city'] . "<p> county is " . $_SESSION['county'] . "<p> area is " . $_SESSION['area'] . "<p> state is " . $_SESSION['state'] . "<p> directions are " . $_SESSION['directions'] . "<p> date is " . $_SESSION['livedate'] . "<p> Time is " . $_SESSION['time'];
if (isset($_SESSION['livedate'])){
$title = $_SESSION['title'];
$typeofsale = $_SESSION['typeofsale'];
$fname = $_SESSION['fname'];
$lname = $_SESSION['lname'];
$email = $_SESSION['email'];
$address = $_SESSION['address'];
$zip = $_SESSION['zip'];
$city = $_SESSION['city'];
$county = $_SESSION['county'];
$area = $_SESSION['area'];
$state = $_SESSION['state'];
$directions = $_SESSION['directions'];
$listitems = $_SESSION['listitems'];
$date = $_SESSION['livedate'];
$time = $_SESSION['time'];
fullListing($title, $typeofsale, $fname, $lname, $email, $address, $zip, $city, $county, $area, $state, $directions, $listitems, $date, $time);
}
if (isset($_POST['onlinedate'])){
$title = $_SESSION['title'];
$typeofsale = $_SESSION['typeofsale'];
$fname = $_SESSION['fname'];
$lname = $_SESSION['lname'];
$email = $_SESSION['email'];
$zip = $_SESSION['zip'];
$city = $_SESSION['city'];
$county = $_SESSION['county'];
$area = $_SESSION['area'];
$state = $_SESSION['state'];
$listitems = $_SESSION['listitems'];
$date = $_SESSION['livedate'];
partialListing($title, $typeofsale, $fname, $lname, $email, $zip, $city, $county, $area, $state, $listitems, $date);
}
function fullListing($title, $typeofsale, $fname, $lname, $email, $address, $zip, $city, $county, $area, $state, $directions, $listitems, $date, $time){
global $dbc;
$insertRow = "INSERT INTO listings(title, typeofsale, fname, lname, email, address, zip, city, county, area, state, directions, listitems, date, time) VALUES ('$title', '$typeofsale', '$fname', '$lname', '$email', '$address', '$zip', '$city', '$county', '$area', '$state', '$directions', '$listitems', '$date', '$time')";
echo "<p>this is whats in insertRow variable " . $insertRow;
$result = $dbc->query($insertRow);
echo "<p>this is whats in insertRow variable " . $insertRow;
}
function partialListing($title, $typeofsale, $fname, $lname, $email, $zip, $city, $county, $area, $state, $listitems, $date){
global $dbc;
$insertRow = "INSERT INTO listings(title, typeofsale, fname, lname, email, zip, city, county, area, state, listitems, date) VALUES ('$title', '$typeofsale', '$fname', '$lname', '$email', '$zip', '$city', '$county', '$area', '$state', '$listitems', $date)";
$result = $dbc->query($insertRow);
echo "this is whats in insertRow variable " . $insertRow;
}
echo "<p><h1>Information Submitted. Thank You!</h1>";
echo "<p>";
include('footer.php');
当它试图运行查询时,它会给我以下错误
Fatal error: Call to a member function query() on a non-object in C:\Program Files\wamp\www\AreaListingPage2\salesent.php on line 52
第52行是这一行:
$result = $dbc->query($insertRow);
我检查以确保db connect和模型中包含正确的信息,并且它们确实包含正确的信息。他们有正确的密码和数据库。然后我检查数据库中的表是否正确,是否正确。我检查了字段的顺序是否正确,是否与数据库字段匹配,结果是正确的 您只需定义一个
全局$dbc
它是一个空变量,不处理与数据库的任何连接,但可以对其调用query()
函数。这行不通
无论您使用什么驱动程序(mysqli、PDO等),都应该正确处理连接。您很容易受到攻击。您是否确定已正确连接到数据库,并且
$dbc
符合您的预期?听起来好像有什么失败了,返回了一个布尔值false,而且由于您完全没有任何错误处理,您的代码只是在前面出错了。我仔细检查了数据库的名称,以便它与我的dbconnect页面匹配。我仔细检查了一下,确定它在找合适的桌子。我确实进行了错误检查,以查看它是否连接到模型中的数据库,并且没有显示任何错误。我仔细检查以确保表匹配名称和顺序。在对其使用query()
之前,先执行var\u dump($dbc)
,查看它是否是正确的数据库对象。看起来确实不是,这意味着问题出在header.php文件中。对不起,dbconnect.php文件。它显示了一个空值。嗯,我注意到我没有关闭dbconnect.php页面中的php,所以我关闭了它,但没有造成任何影响。奇怪的是,它在连接到数据库的一个页面上工作得非常好,但在连接到数据库的下一个页面上却不行。这肯定是某个地方的语法错误。相比之下,我知道这是一个文件,它可以工作,因为我可以测试数据并看到它被插入到数据库中。我对这两个项目使用相同的代码。函数insertIntoTable($fname,$lname,$age,$city,$state){global$dbc;$insertRow=“插入人(fname,lname,age,city,state)值(“$fname”,“$lname”,“$age”,“$city”,“$state”)”;$result=$dbc->query($insertRow);}include('header.php');
$result = $dbc->query($insertRow);