Skip to content Skip to sidebar Skip to footer

Php Register To Database

I am very new to stackoverflow. I want to create this form which uploads information to the database. When I click submit it does not get uploaded. I have checked my connections fi

Solution 1:

Here is the code you need to use:

<?phpinclude("/connections/db_conx.php");
    if(isset($_POST['submit'])) {
        $title   = mysqli_real_escape_string($db_conx, $_POST['title']);
        $text    = mysqli_real_escape_string($db_conx, $_POST['text']);
        $picture = mysqli_real_escape_string($db_conx, $_POST['picture']);
        $sql     = "INSERT INTO news (`title`, `text`, `picture`) VALUES('$title','$text','$picture');";
        if(!$result = $db_conx->query($sql)){
            die('There was an error running the query [' . $db_conx->error . ']');
        }
        echo'Entered into the news table';
    }
?><html><head></head><body><formmethod="post"action="index.php"id="tech"><tableborder="0"><tr><td>Title</td><td><inputtype="text"name="title"></td></tr><tr><td>Text</td><td><textarearows="4"name="text"cols="50"name="comment"form="tech"></textarea></td></tr><tr><td>Picture</td><td><inputtype="varchar"name="picture"></td></tr><tr><td><inputid="button"type="submit"name="submit"value="Submit"></td></tr></table></form></body></html>

Your problem is that the mysqli_real_escape_string() function requires 2 parameters: the database connection, and the string to escape.

I've also included completely reformatted code, and error checking as well.

Solution 2:

Try This :

$title = mysqli_real_escape_string($db_conx, $_POST['title']);
$text = mysqli_real_escape_string($db_conx, $_POST['text']);
$picture = mysqli_real_escape_string($db_conx, $_POST['picture']);

$sql = "INSERT INTO news  (title, text, picture) VALUES('$title','$text','$picture')";

$query = mysqli_query ($db_conx, $sql);

if($query){
echo'Entered into the news table'; // Your Success Message
}
else { 
    echo mysqli_error($db_conx); 
}

Post a Comment for "Php Register To Database"