Inserting fields to MySQL Database through PHP not working - php

I am trying to insert values from gravity form submissions into mysql database using php, but nothing is showing up in my database when I try it:
Here is my code:
add_action("gform_after_submission_17", "push_fields", 10, 2);
function push_fields($entry, $form){
$title = $entry[1];
$location = $entry[1];
global $connection;
$query = ("INSERT INTO events(title, location) VALUES ($title, $location)");
$result = $connection->query($query);
}
I have already connected to the database, as I have used other functions that are pulling data already in the database:
$servername = '127.0.0.1';
$username = '*******';
$password = '*******';
$dbname = '*******';
$connection = new mysqli($servername, $username, $password, $dbname);
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
Please let me know if there is any other info you need.
EDITED (TO TEST FOR ERRORS):
add_action("gform_after_submission_17", "input_fields", 10, 2);
function add_entry($entry){
$title = mysql_real_escape_string($entry[1]);
$location = mysql_real_escape_string($entry[2]);
global $connection;
$query = ("INSERT INTO events(title, location) VALUES ('$title' , '$location')");
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
if($result){
echo 'Query OK';
}else{
echo 'Query failed';
}
}

This may be unhelpful if you're deliberately connecting to the DB; however, you might consider using the GFAPI.
https://docs.gravityforms.com/api-functions/#add-entry

Related

Select data from a row in database

I am making a profile page in which I want the user's email to be displayed. I thought this would be quite a simple code that could be achieved using the select function from the database. However, this only works for one string and I cannot seem to figure out why.
This is my original code
session_start();
$_SESSION["user"] = $username;
$_SESSION["pass"] = $password;
$_SESSION["email"] = $email;
$connection = mysqli_connect ("localhost", "root", "", "picshare");
if ($connection ->connect_error) {
die("Connection failed: " . $connection->connect_error);
}else{
$query = mysqli_query($connection, "SELECT email FROM login WHERE username='".$_SESSION["user"]."'");
$field = mysqli_fetch_assoc($query);
if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);
}else{
echo "error";
$conn->close();
}}
When I try and echo $field, nothing was echoed
<p class ="right uc"><?php echo($field['email']);?></p>
I retried the code, but instead of using a session, I made a variable
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "picshare";
$user = 'Eniola Olaogun';
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$query = mysqli_query($conn, "SELECT email FROM login WHERE username='".$user."'");
$field = mysqli_fetch_assoc($query);
if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);
echo($field['email']);
}else{
echo "error";
$conn->close();
This code displayed the email, and so I proceeded to change the $user variable to another name and the original problem occurred where nothing was echoed.
I went back to the original code and I logged in as Eniola Olaogun and the email was echoed, but as soon as I changed the person I logged in as, no email was echoed.
I am not sure why I am experiencing this problem and some help would be greatly appreciated
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "picshare";
$user = 'Eniola Olaogun';
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
$sql = "SELECT email FROM login WHERE username= {$user}";
$result = $conn->query($sql);
if($result->num_rows > 0) {
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
echo($row['email']);
}
else {
echo "error";
$conn->close();
error connecting to mysql database through crazy domains
It sounds like it is a permission issue based on the user you're logging in as.
Test it with a 'root' user password that has global access and then troubleshoot and isolate it from there. I'm betting you will find it then.
Pretty much impossible for me to test this remotely since I don't have your DB schema and user accounts to validate with.

Cant connect php to mysql

New to php and am connecting form attributes to php to connect to a godaddy mysql. Every attempt ends in a blank screen with no error messages. Is there any syntax errors the jump out? My sublime text wont register php syntax, but thats another problem for another time. I may need to call up godaddy support? the password has been removed for privacy.
<?php
$servername = "localhost";
$dbusername = "jaysenhenderson";
$dbpassword = "xxxxx";
$dbname = "EOTDSurvey";
$con = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
mysql_select_db('EOTDSurvey', $con)
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo("Connected successfully");
$_POST['BI1']
$_POST['BI2']
$_POST['BI3']
$_POST['BI4']
$_POST['BI5']
$_POST['BI6']
$_POST['BI7']
$_POST['BI8']
$_POST['BI9']
$_POST['BI10']
$_POST['BI11']
$_POST['BI12']
$_POST['BI13']
$_POST['BI14']
$_POST['BI15']
$sql = "INSERT INTO Survey1(BI1)"
$sql = "INSERT INTO Survey1(BI2)"
$sql = "INSERT INTO Survey1(BI3)"
$sql = "INSERT INTO Survey1(BI4)"
$sql = "INSERT INTO Survey1(BI5)"
$sql = "INSERT INTO Survey1(BI6)"
$sql = "INSERT INTO Survey1(BI7)"
$sql = "INSERT INTO Survey1(BI8)"
$sql = "INSERT INTO Survey1(BI9)"
$sql = "INSERT INTO Survey1(BI10)"
$sql = "INSERT INTO Survey1(BI11)"
$sql = "INSERT INTO Survey1(BI12)"
$sql = "INSERT INTO Survey1(BI13)"
$sql = "INSERT INTO Survey1(BI14)"
$sql = "INSERT INTO Survey1(BI15)"
if ($conn->query<$sql) === TRUE) {
echo "IT FUCKING WORKS.";
}
else{
echo "didnt workkkkkk";
}
$conn->close();
?>
please connect database like this...
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
And Use mysqli_select_db instead of mysql_select_db
And insert semi-colon (;) after every line end according to php code standard.
There are a lot of issues with this code, as mentioned the mysqli_select_db issue. The $_POST['BIx'] will also cause errors because there is no semi-colon after each statement. You're missing a '(' on the line if ($conn->query<$sql) === TRUE) { not to mention that line will not work anyway because you're logically comparing a resource type (I think) to a string.
You're also never executing the insert statements. All around I seriously think you should practice PHP coding some more and read up on how to use mysqli properly: see here.
Regards
EDIT: You also have a closing PHP tag at the end of your script which is generally not a good idea as explained here
EDIT 2: Also using an IDE such as Netbeans is always a good idea as it can highlight syntax errors instead of asking SO to do it for you ;)
<?php
$servername = "localhost";
$dbusername = "jaysenhenderson";
$dbpassword = "xxxxx";
$dbname = "EOTDSurvey";
$con = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
mysqli_select_db('EOTDSurvey', $con);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo("Connected successfully");
############# Function For Insert ##############
function insert($tableName='',$data=array())
{
$query = "INSERT INTO `$tableName` SET";
$subQuery = '';
foreach ($data as $columnName => $colValue) {
$subQuery .= " `$columnName`='$colValue',";
}
$subQuery = rtrim($subQuery,', ');
$query .= $subQuery;
pr($query);
mysqli_query($con,$query) or die(mysqli_error());
return mysqli_insert_id();
}//end insert
#########################################
if(isset($_POST['submit'])){
unset($_POST['submit']);
//print_r($_POST);
$result=insert('Survey1',$_POST);
if($result){
echo '<script>window.alert("Success!");</script>';
echo "<script>window.location.href = 'yourpage.php'</script>";
}
}
$conn->close();
?>

php delete record using id

This program is meant to delete a record when given the id.
php:
if ($_GET['type']=="file"){
$servername = "localhost";
$username = "****";
$password = "****";
$dbname = "****";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_error($conn)) {
die("Connection failed: " . mysqli_connect_error($conn));
}
$sql = "SELECT id,user, FROM CreationsAndFiles WHERE id =".$_GET['id']." LIMIT 1";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
if ($row['user'] == $login_session){
$sql = "DELETE FROM CreationsAndFiles WHERE id=".$_GET['id'];
if(mysqli_query($conn, $sql)){echo "deleted";}
}
mysqli_close($conn);
//header("location: index.php?page=CreationsAndFiles");
}
the header is type=file&id=9
there is a record where id=9
It for no apparent reason will not work.
Your SQL syntax is wrong;
SELECT id,user, FROM CreationsAndFiles...
^ extra comma
should be simply
SELECT id,user FROM CreationsAndFiles...
You may want to sanitize your input though, for example simply entering type=file&id=id will most likely do bad things.

mysql will not insert past fourth row

I'm not exactly sure what happened but this database and the php effecting it were working just fine until it hit the fourth row and now it won't insert new records at all.
if($_POST)
{
$servername = ******;
$username = ******;
$password = ******;
$db = ******;
$conn = mysqli_connect($servername, $username, $password, $db);
mysqli_select_db($conn,$db);
$uuid = $_POST['uuid'];
$sql = "INSERT INTO uuid VALUES ('$uuid');";
mysqli_query($conn,$sql);
mysqli_close($conn);
}
I'm not sure what happened but this is the relevant code for the mysqli query.
try this
<?php
if(isset($_POST['uuid']))
{
$servername = yourServerName;
$username = username;
$password = password;
$dbname = databaseName;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$uuid = $_POST['uuid'];
$sql = "INSERT INTO tableName (columnName) VALUES ('$uuid')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Also, I recommend using prepared statements.

Simple insert into database doesn't work

I have a very simple bit of code to insert data into database.
It doesn't work. I do not get any error except cannot insert. I could connect to the database so that is not the issue.
The database has 3 fields, emailadd is the 2nd field. The other 2 are auto increment id and creation date, so also a field that on add, it will add current timestamp.
$inquiry = "INSERT INTO subscribe (emailadd) VALUES ('$myemail')";
$res = mysql_query($inquiry) or die("cannot insert");
$inquiry = "INSERT INTO `subscribe` (`emailadd`) VALUES ('$myemail')";
$res = mysql_query($inquiry,$con) or die('Not Inserted : ' . mysql_error());
Some time we need $con variable to identify the database connection, let see more check here
the mysql_query is deprecated, you need to use mysqli like so :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>

Categories