PHP不保存数据到数据库 [英] PHP not saving data to database

查看:196
本文介绍了PHP不保存数据到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我越来越接近找出为什么PHP 不会将数据保存到我的数据库
我从许多教程中学习PHP和MySQL,都失败了。

I feel like I'm getting closer to figuring out why PHP is not saving data to my database. I've tried learning PHP and MySQL from numerous tutorials and all have failed me.

所以...我觉得可能有些东西,我没有指定当尝试连接到MySQL数据库。

So... I feel like there may be something that I haven't been specifying when trying to connect to a MySQL database.

在最近的教程中,只是将输入中的文本输出到MySQL中的表,我收到一条错误,指出找不到服务器localhost。
我的Apache已经安装在端口60(而不是默认端口80)。所以我想这可能是问题。我尝试向 mysqli_connect 添加 localhost:60 ,而不是 localhost 本身和错误消失了!

In a recent tutorial that simply outputs text from an input to a table in MySQL, I got an Error stating that the server "localhost" was not found. My Apache has been installed on port 60 (not the default port 80). So I figured that that might be the problem. I tried adding localhost:60 to the mysqli_connect rather than localhost itself and the error disappeared!

仍然有一个问题: 1.它永远加载demo.php页面代码如下)。 2.数据仍然没有被添加....

There is still a problem though: 1. It takes forever to load the demo.php page (see code below). 2. The data still isn't being added....

这里是代码(我从原来的MySQL视频,以及添加的评论):

Here is the code (I converted it from the original MySQL on the video, to MySQLi and added comments):

demo.php:

   <?php

    define('DB_NAME', 'forms1');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost:60');

    // stored in a variable to TEST if it's working
    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_HOST);

    // TEST if a link has been established (connection)
    if (!$link) {
        die('Could not connect:' . mysqli_error($link));
    }
    // same as above
    $db_selected = mysqli_select_db($link,DB_NAME);

    if(!$db_selected) {
        die('Can\t use ' . DB_NAME . ': ' . mysqli_error($link));
    }
    // Check SUCCESS with commented command below
    // echo 'Connected successfully.';

    // stored in a variable to shorten
    $value = $_POST['input1'];

    // stored in a variable to TEST
    $sql = "INSERT INTO demo (input1) VALUES ('$value')"; 

    if(!mysqli_query($link, $sql)) {
        die('Error: ' . mysqli_error($link));
    }

    mysqli_close($link);
?>

demo-form.php:

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

我也遇到了同样的问题线程在这里:
PHP数据库将不保存数据

I've also had the same problem with another code, see the thread here: PHP database won't save data

真的希望,有人可以帮助我在这里。
这是一个耻辱,我还没有得到基础工作...

I really hope that someone can help me here. It's a shame that I haven't even gotten the basis to work yet...

谢谢!

推荐答案

尝试一下:(您的代码不适用于我)HTML表单和PHP / SQL是一体的。

Try this out: (your present code did not work for me) HTML form and PHP/SQL are all-in-one.

<?php
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');  
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$link = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

if(isset($_POST['submit'])){

    // stored in a variable to shorten
    $value = mysqli_real_escape_string($link,$_POST['input1']);

    // stored in a variable to TEST
    $sql = "INSERT INTO demo (input1) VALUES ('$value')"; 

    if(!mysqli_query($link, $sql)) {
        die('Error: ' . mysqli_error($link));
    }

    else { echo "Success"; }

} // if(isset($_POST['submit']))

    mysqli_close($link);
?>

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

这篇关于PHP不保存数据到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆