dongqing483174 2015-09-09 06:16
浏览 97
已采纳

连接数据库并插入数据 - Php和MySQL -

I'm starting to study php and mysql, I start with this simple module that include:

  1. Create a Database connection; (run)
  2. Connect to DataBase; (not give output message, probably not work)
  3. Insert data; (not work)

Question:
Why it is not working correctly?

I create this simple PHP page (filename: inizio.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="it" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Pagina di inizio</title>
</head>

<body style="width: 672px; height: 289px">

<p class="auto-style1" style="width: 293px; height: 24px">Titolo centrato della pagina</p>
        <form method="post" action="inserimento.php" style="width: 674px; height: 130px">
        Inserisci il nome:<input name="nome" type="text" /><br />
        Inserisci il cognome:<input name="cognome" type="text" /> <br />
        <br />
        <input name="Submit1" type="submit" value="Invia" /><br />
    </form>
</body>
</html>

After I create this php script (filename inserimento.php):

<?php
$host='localhost';
$user='root';
$password='root';
$database='test_youtube';

// 1. Create a database connection
$connection = mysqli_connect($host,$user,$password);
if (!$connection)
{
    die("Database connection failed: " . mysqli_error());
}
else
{
    die("...Connesso con successo al Server!");
}

// 2. Select a database to use 
$db_select = mysqli_select_db($connection, $database);
if (!$db_select)
{
    die("Database selection failed: " . mysqli_error());
}
else 
{
    die("...Connesso con successo al Database!");
}

$query="insert into users(firstname, lastname)VALUES('".$_REQUEST['nome']."','".$request['cognome']."')";
if (!mysqli_query($query,$db_select))
{print("errore inserimento dati");
}
?>
  • 写回答

2条回答 默认 最新

  • doushan7997 2015-09-09 06:22
    关注

    Try die() like following in your php file:

    $connection = mysqli_connect($host,$user,$password,$database);
    
    /* check connection */
    if (mysqli_connect_errno()) {
        die("Connect failed: ", mysqli_connect_error());
    }
    
    $query="insert into users(firstname, lastname)VALUES('".$_REQUEST['nome']."','".$_REQUEST['cognome']."')";
    if ($result = mysqli_query($connection ,$query))
    {
        print("Success!");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)