Kumi Rin Kumi Rin - 2 months ago 6
PHP Question

Form won't save anything in database

My form won't save in my db
connect code saved as con_mysql.php:

<?php
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PSWD', '*****');
DEFINE ('DB_NAME', 'lexusdb');

$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);
?>


form code:

<form method="post" action="newep.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New Content:</legend>
<label>Name: <input type="text" name="newcontent" /></label>
</fieldset>
<br />
<input type="submit" value="add new anime" />
</form>
<?php
echo $newrecord
?>


PHP:

<?php

if (isset($_POST['submit'])) {
include('con_mysql.php');

$nanime = $_POST['newcontent'];
$sqlinsert = "INSERT INTO title (title_name) VALUES ('$newcontent')";

if (!mysqli_query($dbcon, $sqlinsert)) {
die('Error inserting new record');
}

$newrecord = "1 anime added";
}

?>


at first it just won't save anything in DB using the form, and now it also have Undefined variable: newrecord

all files saved in the same folder and newep.php is also created. my db consist of table named title, inside title have title_id INT(4) not null auto_increment then title_name VARCHAR(255) not null. I hope you guys can help me with this one as you guys have help me by just searching for what i need THANKS

Answer

Try this :

<html>
<head>
</head>
<body>
    <form method="post" action="">
        <input type="hidden" name="submitted" value="true" />
        <fieldset>
            <legend>New Content:</legend>
            <label>Name: 
            <input type="text" name="newcontent" /></label>
        </fieldset>
        <br />
        <input type="submit" name="submit" value="add new anime" />
    </form>
    <?php

    if (isset($_POST['submit'])) 
        {
            include_once('con_mysql.php');

            $nanime = $_POST['newcontent'];
            $sqlinsert = "INSERT INTO title (title_name) VALUES ('$nanime')";

            if (!mysqli_query($dbcon, $sqlinsert)) 
            {
                die('Error inserting new record');      
            }

            else
            {
                $newrecord = "1 anime added";
                echo $newrecord;
            }

        }

    ?>

</body>
</html>
Comments