简体   繁体   English

无法将PHP连接到XAMPP数据库以发送数据

[英]Can't connect PHP to XAMPP Database for sending data

Hello I've created a database in myPHPadmin and I'm trying to send registration data over there via an HTML form. 您好,我已经在myPHPadmin中创建了一个数据库,并且试图通过HTML表单在那儿发送注册数据。 I've created the connection files for the server, and have tested them. 我已经为服务器创建了连接文件,并对其进行了测试。 It says I'm connected to the database but when I'm using the form to send the data it doesn't work. 它说我已经连接到数据库,但是当我使用表单发送数据时,它不起作用。

This is the connection file and the index one (the testing that I'm connected to the data base: 这是连接文件和索引一(我连接到数据库的测试:

 <?php

function OpenCon()
{
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '0000';
    $db = 'mydatabase';

$conn = new mysqli($dbhost, $dbuser, $dbpass, $db) or die ("Connect failed: %s\n". $conn -> error);

return $conn;
}

function CloseCon($conn)
{
    $conn -> close();
}

?>

This is the index file. 这是索引文件。 It states Connected Successfully once I run it. 运行它后,它指出已成功连接。

    <?php

include 'connection.php';

$conn = OpenCon();

echo "Connected Successfully";

CloseCon($conn);

?>

I've written the registration form in HTML and combined it via the action method as stated below: 我已经用HTML编写了注册表,并通过如下所述的action方法将其合并:

<form id='register-form' action="database/reg.php" method='post'>
            <input type="text" maxlength="15" name="usr" placeholder="Username" required>
            <input type="text" name="fname" placeholder="First Name" required>
            <input type="text" name="lname" placeholder="Last Name" required>
            <input type="email" name="mail" placeholder="Email" required>
            <input type="password" name="psw" placeholder="Password" required>
            <input type="password" placeholder="Re Password" required>
            <button type='submit'> Εγγραφή</button>

I've payed attention on where to place the files (the directories etc.) But once I enter the data it displays this (Mainly the PHP file that's supposed to run) Code of PHP instead of Alert Window 我已经注意了文件(目录等)的放置位置,但是一旦输入数据,它就会显示此内容(主要是应该运行的PHP文件) PHP代码而不是Alert Window

Can someone please help me or point out what I'm missing? 有人可以帮助我或指出我所缺少的吗?

This is the file handling the transaction: 这是处理事务的文件:

    <?php

include 'connection.php';

$a=$_POST["fname"];
$b=$_POST["lname"];
$c=$_POST["usr"];
$d=$_POST["psw"];
$e=$_POST["mail"];



$con = OpenCon();


$query="INSERT INTO users(firstname,lastname,username,password,email) VALUES ('$a','$b','$c','$d','$e')";

if(mysqli_query($con, $query)){
    echo "<script>alert('Success') </script>";
}

CloseCon($con);

?>

You must use mysqli_query instead of mysql_query : 您必须使用mysqli_query而不是mysql_query

if(mysqli_query($con, $query)) {

because you've connected through mysqli. 因为您已经通过mysqli连接。

I think your query for inserting data is syntactically wrong.It should be like this... 我认为您的查询插入数据在语法上是错误的,应该像这样...

You have to enclose the column name inside backtick symbol ie (``) 您必须将列名放在反引号内,即(``)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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