Apps Tester Apps Tester - 5 months ago 20
SQL Question

Post function is not working in php

I'm just a beginner on php and learning how to code. I'm following a cbt nugget where I'm already on a an action to post the inserted value on a form and pass it to action page and insert to database. I getting this message when I try on the submit button

0Problems with query:

My DB connector

<?php

$host = "localhost";
$user = "root";
$password = "";
$db = "linuxcbtcontacts";

# Step 1 instantiate DB object
$conn = mysqli_connect($host,$user,"",$db) or die("Problems connecting:" . mysql_error());
#if ($conn) { echo "True"; }

#$dbselect = mysqli_select_db("linuxcbtcontacts", $conn) or die("Error selecting DB:" . mysql_error());

# Step 2 - select DB
#$dbselect = mysqli_select_db($conn, "linuxcbtcontacts") or die("Error selecting DB:" . mysql_error());
$dbselect = mysqli_select_db($conn,$db) or die("Error selecting DB:" . mysql_error());

?>


My form action page (after hitting the submit from the form)

<?php
require 'dbconnect.php';


$unique_check = "select name from contacts where name = 'name'";
$results_unique_check = mysqli_query($conn,$unique_check);
$rowcount = mysqli_num_rows($results_unique_check);
echo $rowcount;

if ($rowcount > 0) {
echo "Please use in different name", "<br>";
echo "<a href=query2.php>go back</a>";
exit(); }
else {
$query1 = "INSERT INTO contacts(name,email,age,yearborn,ratesite,industry,buyourproducts,software,hardware) VALUES('$_POST[name]','$_POST[email]','$_POST[age]','$_POST[yearborn]','$_POST[ratesite]','$_POST[industry]','$_POST[buyourproducts],'$_POST[software]','$_POST[hardware]')";

# Step 3 - invoke query
$results = mysqli_query($conn,$query1) or die("Problems with query:" . mysql_error());

$rowcount = mysqli_affected_rows($conn);
echo "Total Inserted Records:\t", $rowcount, "<br>";

}


?>


From the cbt I should get this message after the submit button was clicked

0Total Inserted Records:1


Already tweaked on all possibilities on what I saw on the internet but still i end on the "0Problems with query:" message

Answer
  $query1 = "INSERT INTO   contacts(name,email,age,yearborn,ratesite,industry,buyourproducts,software,hardware)    VALUES('".$_POST['name']."','".$_POST['email']."','".$_POST['age']."','".$_POST['yearborn']."','".$_POST['ratesite']."','".$_POST['industry']."','".$_POST['buyourproducts']."','".$_POST['software']."','".$_POST['hardware']."')";
Comments