form1.php
<?php
$connection = mysql_connect("localhost","root","")
or die ("Couldn't Connect To Server");
$query = "CREATE DATABASE IF NOT EXISTS db1";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$db = mysql_select_db("db1", $connection)
or die ("Couldn't Select Database");
$query = "CREATE TABLE IF NOT EXISTS table1 (fname VARCHAR(20), lname VARCHAR(20), mail VARCHAR(20))";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
function insertvalues (){if ( isset ($_POST['fname']{0}, $_POST['lname']{0}, $_POST['mail']{0}) ){
$query = "INSERT INTO table1 (fname, lname, mail) VALUES ('".$_POST[fname]."', '".$_POST[lname]."', '".$_POST[mail]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
}
else{
echo "No Values Entered. Please Press Back In Your Browser And Enter Some Values.";
}}
$value1 = "";
if isset($value1) {$query = "SELECT * FROM table1";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
echo "<TABLE BORDER = '1'>";
echo "<TR>";
echo "<TH>First Name</TH><TH>Last Name</TH><TH>Mail</TH>";
echo "</TR>";
while ($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>", $row['fname'], "</TD><TD>",
$row['lname'], "</TD><TD>",
$row['mail'], "</TD>";
echo "</TR>";
}
echo "</TABLE>";}
else {
echo "<BR>No Table".
}
mysql_close($connection);
?>
我正在考虑放入$value1
内的内容。如何检查“没有输入值。请在浏览器中按回并输入一些值”。显示错误,如果是,则不显示表格?
答案 0 :(得分:1)
这就是你要求的吗?
<?php
$connection = mysql_connect("localhost","root","")
or die ("Couldn't Connect To Server");
$query = "CREATE DATABASE IF NOT EXISTS db1";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
$db = mysql_select_db("db1", $connection)
or die ("Couldn't Select Database");
$query = "CREATE TABLE IF NOT EXISTS table1 (fname VARCHAR(20), lname VARCHAR(20), mail VARCHAR(20))";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
function insertvalues (){if ( isset ($_POST['fname']{0}, $_POST['lname']{0}, $_POST['mail']{0}) ){
$query = "INSERT INTO table1 (fname, lname, mail) VALUES ('".$_POST[fname]."', '".$_POST[lname]."', '".$_POST[mail]."')";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
return true;
}
else{
echo "No Values Entered. Please Press Back In Your Browser And Enter Some Values.";
return false;
}}
$value1 = insertvalues();
if isset($value1) {$query = "SELECT * FROM table1";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
echo "<TABLE BORDER = '1'>";
echo "<TR>";
echo "<TH>First Name</TH><TH>Last Name</TH><TH>Mail</TH>";
echo "</TR>";
while ($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>", $row['fname'], "</TD><TD>",
$row['lname'], "</TD><TD>",
$row['mail'], "</TD>";
echo "</TR>";
}
echo "</TABLE>";}
else {
echo "<BR>No Table".
}
mysql_close($connection);
?>
代码中的差异是如果成功插入则返回true的返回值,否则返回false。并将该值赋予$ value1。
如果我可以......这段代码有一些严重的性能,其他以电子邮件开头的问题通常需要超过20个字符,并运行一个create db,在每次运行时创建表查询等等。