So, I have this system where you can make a post. So, how it works is, there is an input field, whatever you type in the input field, and click post, it will send to the database as en entry and get posted. The post will be displayed. However, with my current system, after entering something in the input field, and clicking post, the entry gets sent to the database, but the post doesn't actually display. For it to display, you need to refresh the page again, which it displays then, and two entries go to the database.
I don't want this to happen. Right when the user enters text into the input field and clicks post, the post should display on the go, you shouldn't have to refresh for the post to be displayed, and only one entry should be sent to the database, not two. Now, I also included my database connection and my insert statements, but here is the code to display the post:
<div class="textPost">
<?php
$sql = "SELECT * FROM posts";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<div class="textpostFormat">
// all the displayed post content
</div>
<?php
}
}
?>
</div>
Insert Statement (post.php):
<?php
session_start();
// Making Connection To The Database
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "root";
$database = "feed";
$connection = mysqli_connect($dbHost, $dbUser, $dbPass, $database) or die ("Sorry, we could not connect to the database");
// Posting System
if (!empty($_POST['postContent'])) {
$post = $_POST['postContent'];
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$sql = "INSERT INTO posts (firstname, lastname, body, date_posted) VALUES (?, ?, ?, NOW())";
$stmt = mysqli_stmt_init($connection);
// nested if statement
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "";
} else {
mysqli_stmt_bind_param($stmt, "sss", $firstname, $lastname, $post);
mysqli_stmt_execute($stmt);
}
} else {
echo "";
}
?>
So, since PHP is a server side language, to make the calls, you would require AJAX to accomplish this problem/situation. The only problem is, I don't know what the AJAX would be. So my question is, for this particular situation, what would the AJAX be? I don't want any tutorials, just the code. Also, I can't try on some AJAX code and ask for help, because I don't even know what it would be in the first place. I don't want to use the PHP approach to solve this problem, I want the AJAX approach, and for that, the AJAX code to solve this situation.
So, please, please spare a few minutes of your time to answer this question. Please. Thanks a lot! I greatly appreciate your help.
Related
I have an arduino setup posting data to a data_post.php form online which records values to a mysql db. The value for username is not being recorded properly in the db, it shows up as null. The other values are fine.
When I use an html form to manually post to data_post.php, i use those echo commands currently commented out and I can see the values being posted and they are fine. I would like to know if I can see the values being posted by the arduino setup. But I dont know how to view that data from my server side. The data used to work fine up until May 2019 more or less. Is there a way to view that data?
I looked in the server logs and just found this line but it doesnt show the values because its POST, not GET:
2021-02-21 12:40:54 Access 205.211.254.213 200 POST /arduinostuff/data_post.php HTTP/1.0
Here is the php file:
<?php
$user = 'myusr';
$password = 'mypwd';
$server = 'localhost';
$database = 'test';
$pdo = new PDO("mysql:host=$server;dbname=$database", $user, $password);
$username=$_POST['username'];
//echo $username;
$age=$_POST['age'];
//echo $age;
$uvindex=$_POST['uvindex'];
$mq2=$_POST['mq2'];
$sql = "INSERT INTO example (name,age,uvindex,mq2,beer) VALUES (:username, :age, :uvindex, :mq2, 'NO')";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":username", $username);
$stmt->bindParam(":age", $age);
$stmt->bindParam(":uvindex", $uvindex);
$stmt->bindParam(":mq2", $mq2);
$stmt->execute();
$result = $stmt->execute(array(':username'=>$username, ':age'=>$age, ':uvindex'=>$uvindex, ':mq2'=>$mq2));
if($result) {
echo "Your text has been posted";
}// end if
else {
echo '0 results';
}// end else
?>
Add to your php code
var_dump($_POST);
or
print_r($_POST);
This will show every value send by POST
** EDIT: I resolved the issue on my own. Thanks for all your help. **
I'm trying to insert image files to my database for testing, and found that my code stopped working (it was able to do what it did before).
When I submit the image to the database it appends the image id, but not the 'username' and 'img_name'(filename) fields - these two fields just show up as empty strings. Can you tell me what's wrong with my code and how I can fix this? Your help is very appreciated
This is a summary of my database:
Database Name: photos
Table Name: images
Row Names: id[primary key], username, img_name
And my HTML and PHP codes for uploading image file to the database:
<form method="post" action="uploadindex5.php" enctype="multipart/form-data">
<input type="file" name="membimg">
<input type="submit" name="membupload">
</form>
if (isset($_POST['membupload'])) {
$username = $_SESSION['username'];
$membupload = $_POST['membupload'];
$membimg = $_POST['membimg']['name'];
$membtarg = "images/".basename($_FILES['membimg']['name']);
$membmuf = move_uploaded_file($_FILES['membimg']['tmp_name'], $membtarg);
$servername = "localhost";
$sroot = "root";
$password = "";
$dbname = "photos";
$conn = mysqli_connect($servername,$sroot,$password,$dbname);
if (mysqli_connect_errno()) {
throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
}
$sql = "INSERT INTO images (username, img_name) VALUES ('$username', '$membimg')";
$result = mysqli_query($conn, $sql);
if ($membmuf) {
$msg = "Image uploaded";
} else {
$msg = "Upload failed";
}
}
I'd put dummy values in for the session and post values just hard code it and see if the PHP code is working and then determine if those variables are even set once i verified my php code works properly. Once you hard code those questionable variables then you can run the PHP page without submiting it with the form or ajax or however you are calling it. The PHP page will report the errors if you have PHP error reporting on. Javascript console may even tell you if there is a 500 internal server error which indicates the PHP script isn't working.
I have this script on my site:
<?php
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if($db_found) {
$SQL = "INSERT INTO users (user, address)
VALUES('".$_GET['username']."','".$_GET['password']."')";
$result = mysql_query($SQL);
mysql_close($db_handle);
print "Records added to the database";
}
else {
print "Database NOT found";
mysql_close($db_handle);
}
?>
I then open this url in my browser:
http://ringkapps.altervista.org/addToDatabase.php?user=ringk&address=test
But instead of inserting "ringk" and "test" in the table, it inserts this:
Can't understand why, any help would be greatly appreciated.
This code is wrong!
$SQL = "INSERT INTO users (user, address)
VALUES('".$_GET['username']."','".$_GET['password']."')";
Replace this.
$SQL = "INSERT INTO users (user, address)
VALUES('".$_GET['user']."','".$_GET['address']."')";
It's not working because you're calling
http://ringkapps.altervista.org/addToDatabase.php?user=ringk&address=test
Which creates $_GET["user"] and $_GET["address"] but you are trying to put in the db $_GET['username'] and $_GET['password'] which don't exist.
You should call:
http://ringkapps.altervista.org/addToDatabase.php?username=ringk&password=test
Plus, read something on security for PHP apps, your code is prone to a lot of vulnerabilities!!!
In the url : http://ringkapps.altervista.org/addToDatabase.php?user=ringk&address=test
We can see user = ringk and address = test.
Where user is the key and ringk it's value.
Where address is the key and test it's value.
You can print all the $_GET value by using var_dump($_GET) and see by yourself what's in it.
My guess is that what you want is to access
$_GET['user'] and $_GET['address']
then just replace the line :
VALUES('".$_GET['username']."','".$_GET['password']."')";
with
VALUES('".$_GET['user']."','".$_GET['address']."')";
or you could update the url to match the code.
i am trying to display html values from my database into a web page. i am using tinymce as text editor. the data already stored as html values but when i tried to display it to a web page, the values display like this. example:
<p>THIS IS <strong>APPLICATION MANAGEMENT</strong></p>
my code to display is
<?php
$user_name = "BLANK";
$password = "BLANK";
$database = "BLANK";
$server = "BLANK";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SELECT * FROM service WHERE service_id=1";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
print $db_field['contents'];
}
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
my question is how can i display that html values exactly like when i am using tinymce. what i mean is i want to display that html value like this:
<p>THIS IS <strong>APPLICATION MANAGEMENT</strong></p>
to this
THIS IS APPLICATION MANAGEMENT
hope anybody can assist me..
You're looking for the problem in the wrong place.
When you put the data into the database in the first place, it looks like your code is expecting plain text input and converting it to HTML before passing it to the query.
Don't do that.
As a hack, you could run the data through htmlspecialchars_decode when you pull it out of the database, but that's not solving the real problem.
Ok so let me start off by saying I'm pretty noob still with PHP. Basically I'm trying to design a user registration and login system that queries the user data from an Oracle SQL database, and the only tutorials that are helpful to me are all for MYSQL. I was wondering if anyone had the time to help me with some PHP coding to get it working.
Any help at all would be much appreciated.
EDIT: Sorry I forgot to mention, I'm trying to get a login page that sets a cookie to keep the users session until they logout. I have tried a few different variations and none of them work what so ever. I need it to work with the registration page I have below.
My code for the user registration at the moment, of which I'm fairly sure is pretty amateur is:
<?php
/* Set oracle user login and password info */
$dbuser = "xxxxxxx"; /* your login */
$dbpass = "xxxxxx"; /* your password */
$db = "xxxxx";
$connect = OCILogon($dbuser, $dbpass, $db);
$salt = "plants";
if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}
// get the max ID in plants table and allocate $ID+1 for the new record
$max_id_stmt = "SELECT max(ID) FROM register_table";
// check the sql statement for errors and if errors report them
$stmt = OCIParse($connect, $max_id_stmt);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCIExecute($stmt);
$ID =0;
if(OCIFetch($stmt)) {
$ID= OCIResult($stmt,1); //return the data from column 1
}else {
echo "An error occurred in retrieving book id.\n";
exit;
}
$ID++;
// Extract form data
$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$address=$_REQUEST['address'];
global $salt;
// Create the SQL statement to add the data. Note: field value should be single quoted
'' if VARCHAR2 type.
$sql = "INSERT INTO register_table VALUES ($ID, '$username', '$password', '$email',
'$address', '$phone')";
$password = md5($salt.$password);
// Add the data to the database as a new record
$stmt = OCIParse($connect, $sql);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCIExecute($stmt);
echo ("<p>Your registration has been successful!</p>
<p>User ID: $ID</P>
<p>Username: $username</p>
<p>Phone: $phone</p>
<p>Address: $address</p>
</p>")
?>
Thank you!
Thank you for clarifying the problem. I think you are perhaps confusing PHP's $_REQUEST variables with the $_SESSION variables. You'll need to establish a new session in order to maintain a user's session state, which in turn (by default, anyway) sets a cookie. You can establish a new session using standard PHP code either by using session_start() or by configuring PHP to establish a new session automatically.
Of course, doing so doesn't marry the current user to the registration data you're passing into the database via the above code; you would do so by associating the generated session ID with that user data. I suggest taking a look at this page of the PHP documentation for a quick primer.