将输入数据保存到mysql。没有错误,没有数据

时间:2015-02-23 15:40:19

标签: php html mysql forms

我有一个表单,我输入数据并从数据库产品列表中选择一个产品。它工作正常。当我按下提交时,我希望输入数据存储在我的其他mysql表中,但是不保存数据。我没有收到任何错误。

我正在使用类似帖子中的一个例子。

MySQL表:

1   id  int(11) 
2   Vardas  varchar(250)    latin1_swedish_ci
3   Pavardė varchar(250)    latin1_swedish_ci
4   Asmens kodas    int(250)    
5   Adresas varchar(250)    latin1_swedish_ci   
6   Telefono nr.    int(250)    
7   El.paštas   varchar(250)    latin1_swedish_ci   
8   prekė   varchar(250)    latin1_swedish_ci
9   Būsena  varchar(250)    latin1_swedish_ci

我的代码:

<?php 
 //include the databse connection

    include('config/setup.php');
 // get the list of products from the database
    $q = "SELECT * FROM prekes";

    $r = mysqli_query($dbc, $q);


?>
  <?php
    // check the form has been submitted
    if (isset($_POST['submit'])) {
        // escape the form fields and assign them to variables
        // validate myname to ensure the user entered data
        if (isset($_POST['name']) && $_POST['name']!='') {
            $name = mysql_real_escape_string($_POST['name']);
        } else {
            // create an error variable array to store errors to display
            $errors[] = 'Please enter your name'; 
        }

        // validate agree unless you want to add 'checked' to one of the values
        if (isset($_POST['lastName']) && $_POST['lastName']!='') {
            $lastname = mysql_real_escape_string($_POST['lastName']);
        } else {
            $errors[] = 'Please tell us if you agree?'; 
        }

        if (isset($_POST['ak']) && $_POST['ak']!='') {
            $ak = mysql_real_escape_string($_POST['ak']);
        } else {
            $errors[] = 'Please tell us if you agree?'; 
        }

        if (isset($_POST['adress']) && $_POST['adress']!='') {
            $adress = mysql_real_escape_string($_POST['adress']);
        } else {
            $errors[] = 'Please tell us if you agree?'; 
        }

        if (isset($_POST['number']) && $_POST['number']!='') {
            $number = mysql_real_escape_string($_POST['number']);
        } else {
            $errors[] = 'Please tell us if you agree?'; 
        }

        if (isset($_POST['email']) && $_POST['email']!='') {
            $email = mysql_real_escape_string($_POST['email']);
        } else {
            $errors[] = 'Please tell us if you agree?'; 
        }

        if (isset($_POST['preke']) && $_POST['preke']!='') {
            $preke = mysql_real_escape_string($_POST['preke']);
        } else {
            $errors[] = 'Please tell us if you agree?'; 
        }

        //if errors found tell the user else write and execute the query
        if ($errors) {
            $message = '<p class="error">We found a problem:</p><ul>';
            foreach($error as $msg){
                $message .= '<li>'.$msg.'</li>';
            }
            $message .= '</ul><p>Please fix the error/s to continue.</p>';
        } else {
            // write the query
            $query = "INSERT INTO uzsakymai (Vardas, Pavardė, Asmens kodas, Adresas, Telefono nr., El.paštas, prekė)";                           
            $query .= " VALUES ('$name','$lastname','$ak','$adress', '$number', '$email', '$preke')";
            // run the query
            mysqli_query($dbc, $query);

        }
    }
?>
<!DOCTYPE html>
<html lang="en">
  <head>

   <script>
function validateForm() {
    var a = document.forms["myForm"]["name"].value;
    if (a == null || a == "") {
        alert("Visi laukai turi būti užpildyti");
        return false;
    }
 var b = document.forms["myForm"]["lastName"].value;
    if (b == null || b == "") {
        alert("Visi laukai turi būti užpildyti");
        return false;
    }
     var c = document.forms["myForm"]["ak"].value;
    if (c == null || c == "") {
        alert("Visi laukai turi būti užpildyti");
        return false;
    }
     var d = document.forms["myForm"]["adress"].value;
    if (d == null || d == "") {
        alert("Visi laukai turi būti užpildyti");
        return false;
    }
     var e = document.forms["myForm"]["number"].value;
    if (e == null || e == "") {
        alert("Visi laukai turi būti užpildyti");
        return false;
    }
     var f = document.forms["myForm"]["email"].value;
    if (f == null || f == "") {
        alert("Visi laukai turi būti užpildyti");
        return false;
    }
     var g = document.forms["myForm"]["preke"].value;
    if (g == null || g == "") {
        alert("Visi laukai turi būti užpildyti");
        return false;
    }
}
</script>
  </head>

  <body>

<div class="container">

    <div class="row" > 
        <div class="col-md-4">
        </div>
    <div class="col-md-4">
        <h1>Forma</h1>
    <form name="myForm"  onsubmit="return validateForm()" method="post">
        <div class="form-group">
    <label for="name">Vardas</label>
    <input type="text" class="form-control" name="name" placeholder="Vardas"  >
  </div>
  <div class="form-group">
    <label for="lastName">Pavardė</label>
    <input type="text" class="form-control" name="lastName"  placeholder="Pavardė" >
  </div>
  <div class="form-group">
    <label for="ak">Asmens kodas</label>
    <input type="text" class="form-control" name="ak"  placeholder="Asmens kodas" >
  </div>
  <div class="form-group">
    <label for="adress">Adresas</label>
    <input type="text" class="form-control" name="adress"  placeholder="Adresas"  >
  </div>
  <div class="form-group">
    <label for="number">Telefono nr.</label>
    <input type="text" class="form-control" name="number"  placeholder="Telefono nr." >
  </div>
   <div class="form-group">
    <label for="email">El. pašto adresas</label>
    <input type="email" class="form-control" name="email"  placeholder="El. paštas" >
  </div>
   <div class="form-group">
    <label for="preke">Prekės pasirinkimas</label>
    <select class="form-control" name="preke" >
        <?php 
        while($preke = mysqli_fetch_array($r)){   //Creates a loop to loop through results
        echo "<option>" . $preke['Pavadinimas'] . "</option>";  //$row['index'] the index here is a field name
}?>


</select>
  </div>


  <button type="submit" class="btn btn-default" >Užsakyti</button>

</form>

</div>
</div>
<div class="col-md-4">
        </div>
        <a href="admin/login.php" class="btn btn-info pull-right" role="button">Prisijungti</a>
    </div>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="../../dist/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>

使用mysqli_error告诉我这个:

   Errormessage: Unknown system variable 'a';
         Warning: mysqli_query(): Couldn't fetch mysqli in C:\wamp\www\IDEA\index.php on line 7

0 个答案:

没有答案