简体   繁体   中英

cannot add to database mysql

I am having trouble with my form adding to my database.

I have made the form and the script but am having trouble implementing them. Any ideas?

Table:

桌子图片

Here is the HTML form:

<form method="post" id="form2" action="home.php?id=<?php echo "$user_id";?>">  </br>
    <h2>What would you like to ask? Rant away!</h2>
    <input type="text" name="title" placeholder="Write a title" size="92px"/>
    <textarea cols= "70px" rows="6px" name="content";>Description...</textarea><br/>
    <select name="topic">
        <option> Select Topic</option>
        <?php getTopics() ;?>
    </select>
    <input type="submit" name ="submitbtn" value="Post to Timeline"/>
</form>

<?php 
    insert_post();?>

Here is the function - the file the function is in, is included in the HTML file using the include command at the top of the file.

function insert_post(){
    if(isset($_POST['submitbtn'])){
        global $con;
        $title=$_POST['title'];
        $content=$_POST['content'];
        $topic=$_POST['topic'];

        $insert="INSERT INTO `posts` ( `user_id`, `topic_id`, `post_title`, `post_content`, `post_date`) 
        VALUES ('$user_id','$topic','$title','$content',NOW()) ";

        $run=mysqli_query($con, $insert);
        if($run){
            echo"<h3>Discussion posted</h3>";
        }
    }
}

EDIT: For reference, the error I get when clicking the submit is a 404 which says "object not found".

EDIT 2: Image of table shows the row is called pos_title whereas my own code says post_title . When code was edited to say pos_title , the same error still arose.

EDIT 3: New errors after implementing some suggested changes:

新错误

$insert="INSERT INTO `posts` ( `user_id`, `topic_id`, `post_title`, `post_content`, `post_date`) 
    VALUES ('" . $user_id . "','" . $topic . "','" . $title . "','" . $content . "',NOW()) ";

Try this code. You should concatenate using the . instead of inserting variables in the string:

$run=mysqli_query($con, $insert);
if($run) {
    echo"<h3>Discussion posted</h3>";
} else {
    echo mysqli_error($con);
}

NOTE: also for debugging purposes you should check mysqli_error($con); if there's an error.

<form method="post" id="form2" action="home.php?id=<?php echo $user_id;?>">  </br>

When you echo a variable there is no need for double quotes. It is a variable not a string.

    <textarea cols= "70px" rows="6px" name="content">Description...</textarea><br/>

And you have a semicolon after name="content" that should not be there as well.

Remove if(isset($_POST['submitbtn'])){ from the functions and add this above the form:

if(isset($_POST['submitbtn'])){
    insert_post();
}
  1. Change action to action="home.php" .
  2. Add this code to form:

     <input value="<?php echo $user_id?>" type="hidden" name="user_id">
  3. Add this code to insert_post function:

     $user_id = (int)$_POST['user_id'];
  4. Replace post_title to pos_title or alter your table.

  5. You don't need the quotes in '$user_id' , '$topic' , but MySQL will still accept it.

I want to thank you all for your time and effort and help. It turned out there had been a syntax error about 400 lines higher than this form at the previous form, where I'd typo'd when closing it. So the question I had asked was unsolvable for you as it turns out the code shared was more or less fine. So very big apologies for my mistake guys and thanks so much for all your help. This has certainly taught me to clean up my html files and not let them get so big!

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