表单验证,用于存储和检索mysql数据库中的数据

时间:2013-09-27 16:29:51

标签: php mysql forms validation post

您好我是一名在线参加IT课程的学生,但我的背景是工程学。我正在开发一个项目来创建一个表单来验证提交和存储以及从mysql检索数据。我拼凑了一些编码和输入字段以及javascript可能不会网格化。表单显示所有字段,如果没有输入则会产生错误,但在提交时,php页面并不表示它检索到任何数据,并且数据库显示它是空的。

这是编码的一部分:

function validateForm()
{
var x=document.forms["offer"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
</script>

</head> 

<body>  

<header id="banner" class="body">
    <nav><ul>
    <h1><a href="#">New Textbook Offer</a></h1>

        <li><a href="#">Home</a></li>
        <li><a href="#">Search</a></li>
        <li><a href="#">Offered Textbook</a></li>
        <li><a href="#">Requested Textbook</a></li>
        <li><a href="#">Contact</a></li>    
    </nav></ul>

</header><!-- /#banner -->

<form id="offer" action="bookoffer.php" method="post" onSubmit= "return validateForm(this.form)">
    <fieldset>
        <legend>Offered Textbook</legend>
        <ol>
            <li>
                <label for=title>Title</label>
                <input id=title name=btitle type=text placeholder="Book Title" required autofocus>

            </li>
            <li>
                <label for=author>Author</label>
                <input id=author name=bauthor type=text placeholder="author" required>
            </li>
            <li>
                <label for=edition>Edition</label>
                <input id=edition name=bedition type=text placeholder="Edition" required>
            </li>
            <li>
                <label for=isbn>ISBN</label>
                <input id=isbn name=bisbn type=text placeholder="ISBN" required>
            </li>
        </ol>

这是php编码:

<?php
  $conn = mysqli_connect("localhost", "itbgam", 
     "mypassword", "bgamble") 
     or die("Cannot connect to database:" . 
         mysqli_connect_error($conn));

  //read what is posted through the form
  if(empty($_POST["tweet"])) 
        die ("Required: date and post length 1 to 255 characters.");
  $tweet = substr($_POST["tweet"], 0, 255);
  $name = $_POST["name"];
  $email = $_POST["email"];
  $phone_number = $_POST["phone_number"];
  $textbook_title = $_POST["textbook_title"];
  $textbook_author = $_POST["textbook_author"];
  $textbook_edition = $_POST["textbook_edition"];
  $textbook_ISBN = $_POST["textbook_ISBN"];
  $fee = $_POST["fee"];
  $days_available = $_POST["days_available"];

  // create prepared statement 
  // one ? for post itself
  // one ? for date
  // NOW() is MySQL return the current date and time
  // see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
  $query = mysqli_prepare($conn, 
     "INSERT INTO bookoffer (name, email, 
   phone_number, textbook_title, textbook_author, textbook_edition, 
   textbook_ISBN, fee, days_available) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW() )")
        or die("Error: ". mysqli_error($conn));

   // bind parameters "s" - string
   mysqli_stmt_bind_param ($query, "ssssssssss", $tweet, $name, $email, 
   $phone_number, $textbook_title, $textbook_author, $textbook_edition, 
   $textbook_ISBN, $fee, $days_available);

   //run the query mysqli_stmt_execute returns true if the 
   //query was successful 
   mysqli_stmt_execute($query)
       or die("Error. Could not insert into the table." 
                   . mysqli_error($conn));
   echo "Your tweet was recorded. It is tweet #";
   mysqli_stmt_close($query);  //for prepared stmts only


echo $tweet;
echo $name;
echo $email;
echo $phone_number;
echo $textbook_title;
echo $textbook_author;
echo $textbook_edition;
echo $textbook_ISBN;
echo $fee;
echo $days_available;

   //free result set 
   mysqli_free_result($result);

   //close connection 
   mysqli_close($conn);

?>

0 个答案:

没有答案