ataboy Posted March 28, 2020 Share Posted March 28, 2020 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/310399-mysqli-insert-from-form/ Share on other sites More sharing options...
Barand Posted March 28, 2020 Share Posted March 28, 2020 You set out with a connection called "$link" then suddenly switch to a non-existent "$conn" !? Perhaps if you read your own code? Quote Link to comment https://forums.phpfreaks.com/topic/310399-mysqli-insert-from-form/#findComment-1575946 Share on other sites More sharing options...
ramiwahdan Posted March 28, 2020 Share Posted March 28, 2020 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(); Quote Link to comment https://forums.phpfreaks.com/topic/310399-mysqli-insert-from-form/#findComment-1575957 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.