Link to home
Start Free TrialLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php and mysqli

This code is working:

<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "xxxxxxx";
$dbname = "ticktock";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO itdocs (title, details, loc, Type)
VALUES ('".$_POST["doc_name"]."','".$_POST["doc_details"]."','".$_POST["doc_location"]."','".$_POST["doc_type"]."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}

$conn->close();
}
?>

Open in new window


What I need to do is add in another section from a second form on the same page which will add data to the same database fields
The field names for demo sake will be name1, name2, name3, name4
Can someone give me a pointer on how to add the second query
I just need the structure
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doctorbill

ASKER

Ok - point taken
But what about my question - can I have a pointer please
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I have so far:
The first set of code between the first php tags works from a form on the page
The second one from a second form on the page does not


<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "ticktock";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO itdocs (title, details, loc, Type)
VALUES ('".$_POST["doc_name"]."','".$_POST["doc_details"]."','".$_POST["doc_location"]."','".$_POST["doc_type"]."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}

$conn->close();
}
?>

<?php
if(isset($_POST["it_doc_name"])){
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "ticktock";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO itdocs (title, details, loc, Type, file_view)
VALUES ('".$_POST["it_doc_name"]."','".$_POST["it_doc_details"]."','".$_POST["it_doc_location"]."','".$_POST["it_upload"]."','".$_POST["it_doc_type"]."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}

$conn->close();
}
?>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The second set of php code does not add the details to the database
This is the error when I get when I submit the form using the second set of php code

array(0) { }
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it
There was a rogue form tag on my page
Sorry to have wasted everyone's time on such a small issue
working now
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Really appreciate everyone's help
I learn so much from you guys
Good working with you all
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks all
completed