$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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句