我想从数据库中分配值
HTML
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db("test_db", $conn);
if(isset($_POST['submit']))
{
if(!empty($_FILES))
{
$allowedExts = array("gif", "jpeg", "jpg", "png","txt");
$temp = explode(".", $_FILES["resume"]["name"]);
$extension = end($temp);
if (($_FILES["resume"]["size"] < 50000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["resume"]["error"] > 0)
{
echo "Error: " . $_FILES["resume"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["resume"]["name"] . "<br>";
echo "Type: " . $_FILES["resume"]["type"] . "<br>";
echo "Size: " . ($_FILES["resume"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["resume"]["tmp_name"];
}
}
else
{
echo "Invalid file\n";
}
$name = $_POST['name'];
$age = $_POST['age'];
$quali = $_POST['quali'];
$state = $_POST['state'];
$country = $_POST['country'];
$msg = $_POST['msg'];
$resume = $_FILES["resume"]["name"];
$sql = "INSERT INTO form ".
"(name,age,quali,state,country,msg,resume) ".
"VALUES('$name','$age', '$quali', '$state','$country','$msg','$resume')";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
move_uploaded_file( $_FILES["resume"]["tmp_name"], "files/".$_FILES["resume"]["name"]);
}
mysql_close($conn);
}
?>
<div>
<form action="<?php $_PHP_SELF ?>" method="POST" enctype="multipart/form-data">
<div>Name:<input type="text" name="name" /></div>
<div style="height:10px"></div>
<div>Age:<input type="text" name="age" /></div>
<div style="height:10px"></div>
<div>Qualification:<input type="text" name="quali" /></div>
<div style="height:10px"></div>
<div>State:
<select name="state">
<option value="tn">TamilNadu</option>
<option value="kl">Kerala</option>
<option value="ka">Karnataka</option>
<option value="ani">Andhara</option>
</select>
</div>
<div style="height:10px"></div>
<div>Country:<input type="text" name="country" /></div>
<div style="height:10px"></div>
<div>Resume:<input type="file" name="resume" /></div>
<div style="height:10px"></div>
<div>Message:<textarea cols="10" rows="5" name="msg"></textarea></div>
<div style="height:10px"></div>
<div><input type="Submit" name="submit" value="Submit" /></div>
</form>
</div>
<div style="height:30px"></div>
<div>
<table class="table">
<tr>
<th>Name</th>
<th>Age</th>
<th>Qualification</th>
<th>State</th>
<th>Country</th>
<th>Resume</th>
<th>Message</th>
</tr>
<?php
$sql1 = "SELECT * FROM form";
$retval1 = mysql_query($sql1,$conn);
while($row = mysql_fetch_array($retval1))
{
$name_s = $row['name'];
$age_s = $row['age'];
$quali_s = $row['quali'];
$state_s = $row['state'];
$country_s = $row['country'];
$resume_s = $row['resume'];
$msg_s = $row['msg'];
?>
<tr>
<td><label name="name_s"><?php echo $name_s; ?></label></td>
<td><label name="age_s"><?php echo $age_s; ?></label></td>
<td><label name="quali_s"><?php echo $quali_s; ?></label></td>
<td><label name="state_s"><?php echo $state_s; ?></label></td>
<td><label name="country_s"><?php echo $country_s; ?></label></td>
<td><label name="resume_s"><?php echo $resume_s; ?></label></td>
<td><label name="msg_s"><?php echo $msg_s; ?></label></td>
</tr>
<?php
}
?>
</table>
</div>
当我提交数据时,它不会显示在表格中并显示错误。刷新后显示正确
这是我的测试程序,不适用于任何应用程序。告诉我一些建议。我是否使用ajax?或任何其他。
答案 0 :(得分:5)
使用mysqli_connect
<?php
$connect = mysqli_connect("localhost","user","password","your_database") or die("Error " . mysqli_error($connect));
$query = "SELECT * FROM form" or die("Error in the consult.." . mysqli_error($connect));
$result = $connect->query($query);
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><label name="name_s"><?php echo $row['name_s']; ?></label></td>
<td><label name="age_s"><?php echo $row['age_s']; ?></label></td>
<td><label name="quali_s"><?php echo $row['quali_s']; ?></label></td>
<td><label name="state_s"><?php echo $row['state_s']; ?></label></td>
<td><label name="resume_s"><?php echo $row['resume_s']; ?></label></td>
<td><label name="msg_s"><?php echo $row['msg_s']; ?></label></td>
</tr>
}
?>
使用mysql_connect
<?php
$connect = mysql_connect("localhost","user","password");
mysql_select_db("your_database", $connect);
$sql = "SELECT * FROM form";
$retval = mysql_query($sql, $connect);
while($row = mysql_fetch_array($retval))
{ ?>
<tr>
<td><label name="name_s"><?php echo $row['name_s']; ?></label></td>
<td><label name="age_s"><?php echo $row['age_s']; ?></label></td>
<td><label name="quali_s"><?php echo $row['quali_s']; ?></label></td>
<td><label name="state_s"><?php echo $row['state_s']; ?></label></td>
<td><label name="resume_s"><?php echo $row['resume_s']; ?></label></td>
<td><label name="msg_s"><?php echo $row['msg_s']; ?></label></td>
</tr>
<?php } ?>
答案 1 :(得分:0)
然后,您应该能够访问'$ name_s'变量,并将其放入您的表中,如:
<td><label name="name_s"><?= $name_s ?></label></td>
<?= $name_s ?>
是输出变量值的快捷方式,输出标签元素内的值。
答案 2 :(得分:0)
<?php
$sql1 = "SELECT * FROM form";
$retval1 = mysql_query($sql1);
while($row = mysqli_fetch_array($retval1))
{
$name_s = $row['name_s'];
$age_s = $row['age_s'];
$quali_s = $row['quali_s'];
$state_s = $row['state_s'];
$resume_s = $row['resume_s'];
$msg_s = $row['msg_s'];
?>
<tr>
<td><label name="name_s"><?php echo $name_s; ?></label></td>
<td><label name="age_s"><?php echo $age_s; ?></label></td>
<td><label name="quali_s"><?php echo $quali_s; ?></label></td>
<td><label name="state_s"><?php echo $state_s; ?></label></td>
<td><label name="resume_s"><?php echo $resume_s; ?></label></td>
<td><label name="msg_s"><?php echo $msg_s; ?></label></td>
</tr>
<?php
}
?>