Jump to content

Recommended Posts

Having issue insertinginto database. The code are below followed by result. thanks for any advice

Form code:

<html>
<body><center>
<font size=+2><b> Insert data into information database</font><br>

<script type="text/javascript">document.write(mmddyy);</script>

    <form action="crud-insert.php" method="post">

        <p><label for="id">id:</label>
            <input type="text" name="id" id="id"></p>
        <p><label for="target">target:</label>
            <input type="text" name="target" id="target"></p>
        <p><label for="purpose">purpose:</label>
            <input type="text" name="purpose" id="purpose"></p>
        <p><label for="user">user:</label>
            <input type="text" name="user" id="user"></p>
 <p><label for="password">password:</label>
            <input type="text" name="password" id="password"></p>
<p><label for="email">email:</label>
            <input type="text" name="email" id="email"></p>
        <p><label for="visits">visits:</label>
            <input type="text" name="visits" id="visits"></p>
        <p><label for="lastused"> lastused:</label>
            <input type="text" name="lastused" id="lastused"></p>
         <input type="submit" name="submit" value="Submit">
   </form></body></html>
-------------------------------------------------------------------
php code:

 <?php
$link = mysqli_connect("localhost", "root", "", "homedb");
 
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }


$id=$_POST['id']; $target=$_POST['target']; $purpose=$_POST['purpose']; $user=$_POST['user'];
$password=$_POST['password']; $email=$_POST['email']; $visits=$_POST['visits'];
$lastused=$_POST['lastused'];

$sql = "INSERT INTO infotbl (id, target, purpose, user, password, email, visits, lastused)
VALUES ('$id', '$target', '$purpose', '$user', '$password', '$email', '$visits', '$lastused')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
---------------------------------------------------------------
this is result:
query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
" . $conn->error; } $conn->close(); ?>

Link to comment
https://forums.phpfreaks.com/topic/310399-mysqli-insert-from-form/
Share on other sites

6 hours ago, ataboy said:

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Hi,

you need to change $conn to $link

you have:

if ($conn->query($sql) === TRUE) {

and have:

$conn->close();

change them to

if ($link->query($sql) === TRUE) {

and

$link->close();

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.