简体   繁体   中英

Add more than one value to a mysql table with php

I got this code

    <form action="MyCurrentPage.php" method="post" >
    <label for="name_of_trainer"> Name Trainer </label>
    <input type="text" name="name_of_trainer" id="name_of_trainer"/>
    <label for="double"> yearly Income </label>
    <input type="text" name="yearly_income id="yearlyincome"/>
<input type= "submit" value="submit" name="submit" />
</form>

?php
 if (isset($_POST['submit'])) {
     $yearly_income_adition=$_POST['name_of_trainer'];
     $yearly_income=$_POST['yearly_income'];
     $mysqli->select_db("trainers");

    $sql="INSERT INTO trainers (titleCD, yearly_income) VALUES ('".$yearly_income_adition."','".$yearly_income.'")";
    $mysqli->query($sql);

     }

?>   

and I am using it to insert new values into my database but it is not working, the values are not being added and I dont get any error. Do I have a syntax error?

try with:

$sql="INSERT INTO trainers (titleCD, yearly_income) VALUES ('".$yearly_income_adition."','".$yearly_income."')";

Notice the last new position of the last '

Also, as @Webeng commented, learn about avoiding MySQL injection in your code . It's very important to have it as a habit.

You are missing two things:

?php should be <?php

and

'".$yearly_income.'")"; should be '".$yearly_income."')";

Let me know if that works for you.

Why do you check this?

if (isset($_POST['submit'])) {

change it to:

if (isset($_POST['yearly_income'])) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM