Very new at this. I submit my input form, no errors come up. When I go to check the entry into myphpadmin, a row was created, but all the fields are blank.
Not sure what is going on as my text code with only 4 entries works.
<?php
$subscribingcompany= $_POST["subscribingcompany"];
$biztype= $_POST["biztype"];
$sitename= $_POST["sitename"];
$siteadd1= $_POST["siteadd1"];
$city1= $_POST["city1"];
$zip1= $_POST["zip1"];
$state1= $_POST["state1"];
$country1= $_POST["country1"];
$biladd1= $_POST["biladd1"];
$city2= $_POST["city2"];
$zip2= $_POST["zip2"];
$state2= $_POST["state2"];
$country2= $_POST["country2"];
$fename= $_POST["fename"];
$lename= $_POST["lename"];
$email1= $_POST["email1"];
$phone1= $_POST["phone1"];
$foname= $_POST["foname"];
$loname= $_POST["loname"];
$email2= $_POST["email2"];
$phone2= $_POST["phone2"];
$effdate= $_POST["effdate"];
$camquantity= $_POST["camquantity"];
$currency= $_POST["currency"];
$quantity= $_POST["quantity"];
$database="greenguy_cbrsandbox";
$table="DesignPartner";
//Create connection and select database
$con= mysql_connect("example.com", "greenguy_ccb", "password88") or die(mysql_error());
echo "connected";
mysql_select_db("$database", $con) or die(mysql_error());
echo"database found";
//Insert into Table
$insert = "INSERT INTO $table
(id, subscribingcompany, biztype, sitename, siteadd1, city1, zip1, state1, country1, biladd1, city2, zip2, state2, country2, fename, lename, email1, phone1, foname, loname, email2, phone2, effdate, camquantity, currency, quantity)
VALUES
(DEFAULT,'$subscribingcompany','$biztype','$sitename','$siteadd1','$city1','$zip1','$state1','$country1','$biladd1','$city2','$zip2','$state2','$country2','$fename','$lename','$email1','$phone1','$foname','$loname','$email2','$phone2','$effdate','$camquantity','$currency','$quantity')";
$results =mysql_query($insert) or die(mysql_error());
echo"data inserted succesfully";
mysql_close($con);
?>
You Insert query is working properly as you have provided the auto increment functionality
row is getting created but rest of the values are not getting inserted please echo your query during execution .
use echo $insert to know what is getting inserted when query is executing.
Related
So I have an HTML form, which is sending data to a database via an "input.php" file which I have connected to my database.
What I wanted to accomplish, is that the data being sent to the database from the HTML form is all numbers. I wanted to know how I can code the input.php file so that when new numbers get submitted from the HTML form, and get sent to a value which already has a number, to add the two up.
For example, Person A fills out a number in the form on day one, per say 5. The next day, he submits the exact same field with the number 3. In the database, instead of overriding the first value, I want to add it up. So on day 1, the database should say "5", but on day 2, it should say "8".
Here is my bare-bones "input.php" file which I will use. The names for the fields will change in my finalized code.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);
$last_name = mysqli_real_escape_string($link, $_REQUEST['last_name']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
// Attempt insert query execution
$sql = "INSERT INTO persons (first_name, last_name, email) VALUES
('$first_name', '$last_name', '$email')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Any help would be appreciated. I thought maybe I could use some javascript validation, however that would be for validating the field, and not adding...so...
Thanks!
Let's say the column name is counts which needs to be summed, and column index id, you could use ON DUPLICATE KEY UPDATE to update based on id if it's already exist, and insert a new row if it's not exist:
UPDATE: You don't need the id field in this situation, as you can automate it using an AUTO_INCREMENT.
// Attempt insert query execution
$sql = "INSERT INTO persons (first_name, last_name, email, counts) VALUES
('$first_name', '$last_name', '$email', '$counts')
ON DUPLICATE KEY UPDATE counts = counts + $counts";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Two things you need to do. First, verify if the record exists or not. Second, If exists update the record by adding the form input to the column . If not exists then insert new record with form input. To update you can write query like
UPDATE TABLE_NAME
SET column_to_update = column_to_update+form_input_number
WHERE email=<email_address> ;
(assuming email is the primary key or replace email with any primary key field name)
I am sending from arduino usinh http 1.1 (x-www-form-urlencoded) temperature and humidity readings to mysql database. if I use commented part of the following code, everything works (tab12 is the name of the table in mysql database). But I would like to arduino send the name of the table, so I could use the same add.php file to play with multiple arduinos. The thing is, I don't understand how to correctly put the table name from $tabid=$_POST["tabid"]; to query.
<?php
include("connect.php");
$link=Connection();
$tabid=$_POST["tabid"];
$temp1=$_POST["temp1"];
$hum1=$_POST["hum1"];
// $query = "INSERT INTO `tab12` (`temperature`, `humidity`)
// VALUES ('".$temp1."','".$hum1."')";
$query = "INSERT INTO `"tabid"` (`temperature`, `humidity`)
VALUES ('".$tabid."','".$temp1."','".$hum1."')";
mysql_query($query,$link);
mysql_close($link);
header("Location: index.php");
?>
You would concatenate just as you would with any variable:
$query = "INSERT INTO `" . $tabid . "` (`temperature`, `humidity`)
VALUES ('".$temp1."','".$hum1."')";
In addition your number of columns and values must match.
I am just starting out with PHP and SQL and I am trying to make a account creation screen for a website in PHP. For this I've created a form file with an action to the second file where the input data from the form is supposed to be inserted into TWO TABLES of a local database. (I am using WAMP with phpMyAdmin) -
My (database) connection is up and everything and I have no errors, and when go to phpMyAdmin and do the SQL statement with dummy data it works, but when the user fills out the form and clicks on next it doesn't get inserted into the database? Anyone have a idea how to fix this?
Here is my current php code where the data is supposed to be inserted into 2 different tables via the SQL statement:
<?php
$_SESSION['user'] = $_POST['name']; //user naam ophalen van vorige pagina en onthouden voor gehele sessie
$name= $_SESSION['user'];
$_SESSION['wachtwoord'] = $_POST['access'];
$access= $_SESSION['wachtwoord'];
$_SESSION['mail'] = $_POST['email'];
$email= $_SESSION['mail'];
$_SESSION['huisdier'] = $_POST['pet'];
$pet= $_SESSION['huisdier'];
$savenewuser = "BEGIN;
INSERT INTO user (id, naam, email, pass)
VALUES (NULL, '$name', '$email', '$access');
INSERT INTO preferences (id, huisdier)
VALUES (LAST_INSERT_ID(), '$pet');
COMMIT;
ROLLBACK;";
mysqli_query($con, $savenewuser);
$result = mysqli_query($con, $savenewuser);
if (!$result) {
echo "Error: Account could not be created, try again later.";
} else {
echo "Account has been created!";
}
mysqli_close($con);
?>
When executed I do get the message "Error: Account could not be created, try again later." of course. - I think I have to change something about the SQL statement but how exactly I am unsure of.
mysqli_query is being called twice. If you call it twice the second time will error because you are trying to give 2 people the same id (Assuming you set your database up correctly).
If that doesn't work, I would try replacing mysqli_query with mysqli_multi_query.
<?php
$con=mysqli_connect("localhost","usr","pwd","db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT id, Name, email FROM users WHERE status='ACTIVE'");
while($row = mysqli_fetch_array($result)){
// echo $row['Name']. " - ". $row['email'];
// echo "<br />";
$userid = $row['id'];
$username = $row['Name'];
$email = $row['email'];
mysqli_query($con, "INSERT INTO other_user (user_id, username, email)
VALUES ($userid, $username, $email)");
}
mysqli_close($con);
?>
i have the above code i am trying to insert data from one table to another
The above code do not returning any error but it do not puts any data to second table "other_user"
There is an error in INSERT query - you have to enclose strings in quotes, like this:
"INSERT INTO other_user (user_id, username, email)
VALUES ($userid, '$username', '$email')"
A single query would be enough:
$result = mysqli_query($con, "INSERT INTO other_user (user_id, username, email)
SELECT id, Name, email FROM users WHERE status='ACTIVE'");
No need for an agonizing slow row by row insert.
PS: The original error was leaving out quotes around your values.
You should use mysqli prepared statement to insert data to table. Now you don't use quotes in your query (probably that's why data is not inserted into second table) and even if you were, it would be still vulnerable to SQL Injection
I think you should carefully check the table design of your new table.
Check if the column names and types are what you expect.
Also user_id in your new table may be an autoincrement index and than if doesn't have to be inserted.
I am trying to insert data into MySQL database. For each of the data been inserted I want the username to be added so as to keep track of the user the insert that data.Here is my sample query below but the username is not inserting.
$username = $_SESSION['log']['username'];
$Query = "INSERT INTO core_modules
(username,courseID,title,credits)
VALUES ('$username',SELECT ID,title,credits FROM module
WHERE ID IN ('CS4150','CS4403','CS4407','CS4501','CS4504','CS4614'))";
mysql_query($Query);
echo $Query. '<br />';
This is what my database looks like and I need the username inserted into each record inserted.
How could I go about this please ?? Thanks in advance.
INSERT INTO core_modules (username,courseID,title,credits)
SELECT '$username', ID, title, credits
FROM module
WHERE ID IN ('CS4150','CS4403','CS4407','CS4501','CS4504','CS4614')