簡體   English   中英

使用表格將數據插入php表

[英]insert data into php table using form

$servername = "localhost";
$username = "******";
$password = "*****";
$dbname = "*****";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
} else {


$sql = "SELECT * from actor";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table border='0'><tr><th>ID</th><th>Nombre</th> <th>Apellidos</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr>"."<td>".$row["cod_actor"]."</td>"."<td>".$row["nombre"]."</td>" ."<td>".$row["apellidos"]."</td>"."</tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

this,當執行時(以及最后的一些CSS),結果如下: http : //i.stack.imgur.com/okrVe.jpg我想要做的是一個包含3個字段的表單:

id: <input type="text" name="id"><br>
nombre: <input type="text" name="nombre">
apellidos: <input type="text" name="apellido"><br>

形成這個,我知道我必須這樣做:

$id=$_POST['id'];
$nombre=$_POST['nombre'];
$apellido=$_POST['apellido'];

所以我使用用戶在這些字段上寫的內容進行查詢:

$sql = "INSERT INTO actores (id, nombre, apellido)
VALUES ('$id', '$nombre', '$apellido')";

if (mysqli_query($conexion, $sql)) {
    echo "Insert successful";
} else {
    echo "there was an error " . mysqli_error($conexion);
}

現在,我的問題是,當用戶輸入表單時,這些變量為空,我認為我必須對isset進行操作,但是我嘗試執行IF,但無濟於事,您會推薦什么?

我也試圖獲取數組中的查詢結果,我認為我必須使用

mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )

混有一段時間。

<?php

if (isset($_POST['id'],$_POST['nombre'], $_POST['apellido'])) {

$id=mysql_real_escape_string($_POST['id']);
$nombre=mysql_real_escape_string($_POST['nombre']);
$apellido=mysql_real_escape_string($_POST['apellido']);

}

?>

<form action="" method="POST">

            <p>
                <input type="text" name="id" />
                <input type="text" name="nombre" />
                <input type="text" name="apellido" />


  <input type="submit" value="Submit" />

            </p>

</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM