Pushing Attachment Path to DB table - php

I've updated my project to it's almost final form and made a small change. The suggestion of #Raghbendra Nayak worked before the change and now, I'm trying to incorporate it to my new form. After 'submit' it pushes the item to the folder still and the link to the DB, however the link is on a separate row and not on the same row where the data name-description written is found.
Update: Got it working guys. Thanks!

You can simply follow the below code to insert data in database why you are using mysqli_connect to run the insert query.
Updated:
Change your if condition:
if(move_uploaded_file($file['tmp_name'],$upload_directory.$path)){
mysqli_connect("localhost", "root", "", "order") or die ("Connecting to DB failed");
mysqli_connect("INSERT INTO item VALUES ('', '$path')");
}
To
if(move_uploaded_file($_FILES['filename']['tmp_name'],$upload_directory.$path)){
$conn = mysqli_connect("localhost", "root", "", "order") or die ("Connecting to DB failed");
// If you want to save image name you can get like below:
$filename = $_FILES["filename"]["name"];
$path = $path."/".$filename;
$sql = "INSERT INTO item (path) VALUES ('$path')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
You have to add your image name also. Hope it will work for you, let me know it anything required.

Related

PHP script to Increment value in mysql

I am trying to design a scoreboard for a project for my local youth club and would like to be able to click a button onto a wordpress page that will run a PHP script to update a value by 1 in a sql db table, then i can grab the value and display it else where.
Not too worried about passwords being used in scripts at this will only be used within the local network that nobody else has access to, have looked around and found a few bits of code but im not able to actually get it working, here's what i've got so far.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "sot";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = UPDATE wp_sotstats SET CaptainChest=CaptainChest+1 WHERE id=1
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
The database name is 'sot' the table i want to update is called 'wp_sotstats' and the field within the table is 'CaptainChest' i only need this to really work with just the one entry which the id is '1'
Any thoughts?
$sql = UPDATE wp_sotstats SET CaptainChest=CaptainChest+1 WHERE id=1
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
Is not creating record, it updating it. For me, I will take current value from database as Select, then do ++ to it and do Update query.
$sql = UPDATE wp_sotstats SET CaptainChest=CaptainChest+1 WHERE id=1
Please wrap it in quotes and finish statement with semicolon

How to give validation with statement of value on database table

i've problem while i make an insert function using php. i dont know how to give a validation while inserting data to database. my insert function using of the other data table from the database (copying data from other table to another).
somebody know how to give a validation with that data. not from a textbox value or input value from the form. but validation from the data in the database.
please help me out my trouble. im very appreciated when you give me an example.
this is my code for insert data (copying data from the other table to another).
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "silo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$sql="Insert into termocouple
Select * from temp1";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>

show id of entered text to database

I'm fairly new to stack overflow. i am creating a site were you type text in to 2 text boxes and it sends it to a database. i need it then to tell me what the id of that was save it as a session and then upload it to another database. sounds confusing. but I'm stuck of one part. its viewing the result thats from just that user. i have tried just showing the the last id of the last uploaded but it can be very unreliable if multiple people are trying to upload data and know there exact session. I'm also having trouble linking the session with the id. below is the code for the forum saving to the database. I'm pretty confident with sending the id of that users inputed data to another database. I'm just stuck on finding that users inputed texts id and creating a session holding the id number
<?php
header("Location:myscorenum.php");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "score";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
session_start();
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "working";
}else{
//3.2 When the user visits the page first time, simple login form will be displayed.
}
$value = $_POST['name'];
$value1 = $_POST['description'];
$sql = "INSERT INTO all_scores (name, description) VALUES ('$value','$value1')";
if ($conn->query($sql) === TRUE) {
echo "<a href=https://twitter.com/angela_bradley>My Twitter</a>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
ill have the processing to inset to another database in another file. I'm confident with uploading a specific session.
any questions don't hesitate to message me. thanks for your kind help.
You can use this:
mysqli::$insert_id -- mysqli_insert_id — Returns the auto generated id used in the last query
Like: echo $conn->insert_id;
Since you call it on $conn which is a mysqli instance that already has a connection, it will return your last inserted id, irregardless of other activities on the db (other queries do not affect the correctness of the output)
I edited my answer. This is the final part of your code and I've just added one line of code to it. Look if this is what you're looking for.
If this isn't working than make sure your database id field is AI.
if ($conn->query($sql) === TRUE) {
$id=$conn->insert_id; //this is where you get the last inserted id.
echo "<a href=https://twitter.com/angela_bradley>My Twitter</a>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
To use a session and set the ->insert_id to a session variable and use it in any other page, you can do this:
if ($conn->query($sql) === TRUE) {
session_start();
$_SESSION['id']=$conn->insert_id;
echo "<a href=https://twitter.com/angela_bradley>My Twitter</a>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
and now in any other page do this to retrieve the session variable:
session_start();
$id=$_SESSION['id'];
here you go and you get the id.
Are you still confused?

Posting Base64 encoded values in phpmyadmin from Android

In my app i am trying to post image from android to phpmyadmin i have also created php code which is here:
MyPhp.php:
<?php
$servername = "dontneedthis";
$username = "also";
$password = "dont care";
$dbname = "bla bla";
$conn =mysqli_connect('localhost', $username, $password);
$db_selected = mysqli_select_db($conn, $dbname);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$base=$_REQUEST['image'];
$filename = $_REQUEST['filename'];
$binary=base64_decode($base);
$sql = "INSERT INTO table (image) VALUES('{$binary}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
When i upload image from android i get an error where it is shown that he doent understand this :
INSERT INTO table (image) VALUES
And he shows a lot of symbols which i do not recognise. I have created table where is a row where you can add 100 000 symbols of TEXT I tried to add the value as blob and tried to change collation to binary nothing worked do you have any ideas?
Why doesn't anyone ever bother to properly quote their stuff?
table is a keyword in all SQL dialects I know, and hence causes a syntax error.
But for that reason, quotes and backticks have been invented.
Do this:
INSERT INTO `table` (`image`) VALUES ...
and you should have one problem less.
Also, you have to escape your $binary variable, otherwise it's gonna break your ' quotes:
$binary = mysqli_real_escape_string($conn, base64_decode($base));

Cannot get information generated from google maps into MySQL

I am trying to insert values for longitude, latitude and descriptions of locations generated from google maps (the ID value is to be generated within PhpMyAdmin. When I run the following code I always get the "error" message. If I "echo $sql;" before //insert values into database then I get the intended output so I guess it must be a problem with transferring to the database.
I'm using phpMyAdmin from within MAMP so am not sure if that is causing any issues of whether I'm missing something obvious in the code? I'm fairly new to PHP so may have missed something obvious! Any help would be much appreciated.
<?php
$con=mysqli_connect("localhost", "root", "root", "googlemaps") or die("could not connent to db");
error_reporting(0);
for($i=0;$i<count($_POST['value']);$i=$i+3)
{
$sql .= "(NULL, '".$_POST['value'][$i]."', '".$_POST['value'][$i+1]."', '".$_POST['value'][$i+2]."'),";
}
//remove last comma
$sql = substr($sql,0,-1);
//insert values into database
$query = "INSERT INTO 'googlemaps'.'values' ('id', 'lat', 'lng', 'des') VALUES " .$sql;
$status = mysqli_query($con, $query);
if($status)
echo "inserted";
else
echo "error";
?>

Categories