I'm using "Bootstrap Editable" with "in line editing".
I'm printing a table with data from MySQL.
Now i´d like to be able to edit a cell and update my database with PHP.
The "in line edit" script works fine.
But the mysql update doesn't.
Now, when i turn on "php error mode" or tries to "JS alert" my variables in post php, to check if they holds data i can't see any errors or alerts on the front page.
How can i read the error codes or alerts?
This is my code:
HTML
echo "<td><a href='#' id='element_ant' data-type='text' data-pk='".$row['id']."' data-url='php/posts.php' data-title='Anteckning..'>".$row['element_ant']."</a></td>";
PHP
if($_POST['name']=='element_ant'){
$id=$_POST['pk'];
$element_ant=$_POST['value'];
//Prepare query
$query = "SELECT COUNT(*) as count FROM table WHERE id=$id";
try{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}
//Result from query
$row = $stmt->fetch();
//Deside insert or update
if($row[0]==0){
$query = "INSERT INTO table(id,element_ant) VALUES(:id,:element_ant)";
}
else{
$query = "UPDATE table SET element_ant = :element_ant WHERE id = :id";
}
// Security measures
$query_params = array(':id' => $id,':element_ant' => $element_ant);
//Connect and execute
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}
}
JS
$(document).ready( function () {
$('#element_ant').editable({
url : '../php/elements.php',
title : 'Enter comments'
});
});
1) The url attribute is duplicated, i.e you have declared it as data-url in your HTML and in your Js code as url:. Decide which one is right and use either, not both.
2) In your php, there is if($_POST['name']=='element_ant'); you have to set it in your editable HTML element as such:
<a href='#' id='element_ant' data-name="element_ant"
When the Editable object is being saved, the data-name will be passed as a POST parameter name.
For Client-Side Debugging: Firebug
For Server-Side Debugging: Xdebug
I think you can use some sort of logging framework , or you can write your own logging functions to log your error code on the file system.
Related
So, I want to show a js alert to the user if they exist in a table called paymentindue. The table has a custom message column. I want the alert to show the custom message. I am using PHP to handle sql queries
<?php
include 'dbconn.php';
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$sql = "SELECT * FROM users where email='$email' AND username = '$name'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0){
echo '<script>alert("<?php Custom message goes here?>")</script>';
}
?>
But the problem is that I get the alert instead of the custom message
You need to split your echo into two strings and a php variable to make this work.
$your_custom_message = 'Your message here'
echo '<script>alert("' . $your_custom_message . '")</script>'`
It looks like you are trying to send php code to the user, which doesn't work. Instead, just compose the message in your script if needed.
$message = "something you compose here";
echo '<script>alert("'. $message .'")</script>';
I have a website whose function is to translate subtitles from one language to another, translation is done using google widget tool , marked with red color on the image: https://ibb.co/LCWt6YV (as you can see on the left it says on google widget tool "Select Language")
there is also the option that the user can save the translated subtitle file to the database and the language in which the user translated the subtitle is automatically saved to the database,now many users do not know to use this option so they do not choose the language to translate the subtitle and just leave the option on google widget tool "Select Language".
I need some function/code so that no action will be done if Language is not chosen.
That if he does not select a language through a google widget tool and tries to save subtitles in the database, the message "Please select a language before saving subtitle" appears.
This php script is for saving subtitles into a database, now which function/code I should add in this script ?
$data = $_POST['data'];
$file_name = $_POST['xxx'];
$subtitle = $_POST['xxx'];
$language = $_POST['xxx'];
$author = $_POST['xxx'];
$ip= $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
$ip = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}
$data =json_decode($data, true);
foreach($data as $obj){
$subtitles1->add($obj['start'], $obj['end'],$obj['text']);
$subtitles1->save($file_name);
}
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_file_name_ext = time()."_".$sub."_".$lan.".".$ext;
$new_file_name = time()."_".$sub."_".$lan;
$sql_lan = "SELECT code FROM lan_code WHERE language ='$lan'";
$result_lan = mysqli_query($conn, $sql_lan);
if (mysqli_num_rows($result_lan) > 0) {
while($row = mysqli_fetch_array($result_lan)){
$lan_code = $row['code'];
}
}else{
$lan_code = $lan;
}
$sub_new = $sub.'_'.$lan_code;
$sql = "SELECT * FROM table WHERE name ='$sub_new'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$data1 = "Unable to save subtitle, subtitle already exists with the same language in the database.";
}
else{
$query1 = "INSERT INTO table (xxx , xxx, xxx, xxx, ip)
VALUES ('$sub_new','$authors', '$lan', '$new_file_name', '$ip')";
if(mysqli_query($conn, $query1)){
copy("$file_name", "uploads/$new_file_name_ext");
//new GoodZipArchive('uploads/$new_file_name', 'upload/output_zip_file.zip') ;
$data1 = "Successful, File saved to database.";
$zip = new ZipArchive;
$new_zip = time()."_".$subtitles."_".$languages.".zip";
if ($zip->open("uploadsk/$new_zip", ZipArchive::CREATE) === TRUE)
{
$zip->addFile("uploads/$new_file_name_ext", "$new_file_name_ext");
$zip->close();
}
} else{
$data1 = mysqli_error();
}
}
echo $data1; ?>
If you want to pop up a message when no language is selected, you need to use Javascript.
In a very simplistic way you can use javascript alert() function, something like this:
echo '<script type="text/javascript">';
if(empty($language) or !isset($language)){
echo 'alert("please select a language")';
}else{
...add the rest of your code here.
}
echo '</script>';
But this results in a very ugly output. If you want something prettier you can use a plugin like sweetAlert. It's very easy to use, and a lot better then simply "alert" your message. You can take a look to sweetAlert here.
Good luck!
I am new to php and I am not sure how to debug this.
I am trying to pass json to a php page and then send that data to mySQL.
I think it is having issues interpreting the data inside the php file or getting the information to the php page. When I open the php file it gives signs that it is properly accessing the database.
Here is my javascript code:
var request = new XMLHttpRequest();
request.open('POST', 'http://website/saveF.php', true);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
request.send(bInfo);
This is taking information in and passing it to a php file to then be added to a mySQL database.
Here is my php code:
This is decoding the jSon and then itterating over each entry inside the array. It then asks the question if it has a website listed or not and stores it into the appropriate table.
//as long as the connection is good then we keep it live.
include_once "head.php";
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
//gettting the information from the front end (index.html)
$inputJSON = file_get_contents('php://input');
//decode all the previously encoded information
$postThings = json_decode($inputJSON, TRUE);
$input = filter_var($postThings, FILTER_SANITIZE_STRING);
//create a variable the is the total length of our array
$totalNum = count($input);
//arrays start at 0
$i = 0;
//you can see where this is going. We have a while loop that will continue as long as i is less than totalnum. Ask me why i didn't use a for loop.... I don't have an answer.
while($i < $totalNum){
$var0 = $input[$i][0];
$var1 = $input[$i][1];
$var2 = $input[$i][2];
$var3 = $input[$i][3];
$var4 = $input[$i][4];
$var5 = $input[$i][5];
$var6 = $input[$i][6];
if($var1 == "Not Listed") {
$sql = "INSERT INTO missing(cName, website, rating, phone, id, address, placeType) VALUES ('$var0', '$var1', '$var2', '$var3', '$var4', '$var5', '$var6')";
}else{
//here we set the information into the database.
$sql = "INSERT INTO companies(cName, website, rating, phone, id, address, placeType) VALUES ('$var0', '$var1', '$var2', '$var3', '$var4', '$var5', '$var6')";
}
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$i++;
}
First, note that this line:
$input = filter_var($postThings, FILTER_SANITIZE_STRING);
Will return FALSE if sanitization fails on any of the array elements. In your code, you should be testing if($input) immediately after the sanitization.
Furthermore, you will want to sanitize your inputs further to avoid SQL injection and XSS attacks. (e.g. remove SQL escape characters and other injectable characters).
http://php.net/manual/en/mysqli.real-escape-string.php
Last, it is recommended that you use bound parameters or fully sanitized inputs to avoid a SQL injection attack.
Background: I am doing ajax calls to a PHP script which returns data to fill a form based on some file key a user inputs. This form is used so users may edit an incorrectly inputted file key from their original submission.
Problem: If a user wanted to edit a file key, they input it into a text box, hit a button for an ajax pull, the form fills, they can then correct their mistakes and submit. However, if they try to edit the new file key again, the form will not fill and I am getting no results returned from the query. This is the php script I have been using to pull the data from the server.
A sample file key might be: 10000010000-0D-MAN.
This is a good response: 10000010000-0D-MAN,N/A,amibaguest,dfgfdgfd,Electrical
This is the response I get on a newly edited file key: Nothing returned. Id: 20000010000-0D-MAN.
Really baffled at the moment. If more information is needed, please let me know.
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("database", $dbhandle)
or die("Could not select database");
$id = $_GET['param'];
if(isset($_GET['param'])) {
$sql = sprintf("SELECT a.File_key, a.Name, a.Uploader, a.File_descriptor, b.keyword1 FROM files as a, keyword as b WHERE a.File_key='%s' AND a.File_key=b.File_key", mysql_real_escape_string($id));
$result = mysql_query($sql);
if($result === FALSE) {
echo mysql_error();
}
if(mysql_num_rows($result)==1) {
while($myrow = mysql_fetch_array($result)) {
$file_key = $myrow["File_key"];
$name = $myrow["Name"];
$uploader = $myrow["Uploader"];
$file_desc = $myrow["File_descriptor"];
$keyword = $myrow["keyword1"];
$text_out .= $file_key.",".$name.",".$uploader.",".$file_desc.",".$keyword;
}// end while
} else {
$text_out = " Nothing returned. Id: ".$id;
}// end else
}// endif
echo $text_out;
When i select an option I call a Javascript code with the onchange function.
I send 2 parameters with it:
//capaciteit.php
function testfunction(week, id)
{
window.location.href = "capaciteitberekening.php?week=" + week + "&id=" + id;
}
I call the file capaciteitberekening.php and send the parameters with it.
then I try to get the both parameters with the $_GET function:
//capaciteitberekening.php
require ('capaciteit.php');
$id = $_GET['id'];
$week = $_GET['week'];
echo $id, $week;
After I echo both $id and $week(to check if they are working) I call a query:
//capaciteitberekening.php
$datumbegin = mysql_query("SELECT * FROM capaciteit");
while($row = mysql_fetch_array($datumbegin))
{
echo $row['DAGEN'];
}
When I try this on my website its only shows the echo of $id and $week.
this code is working code since I use it somewhere else and it works fine there.
this is the way i connect to my DB:
require('auth.php');require_once('config.php');require_once('exec/pubs_exec.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); //Database openen
if(!$link) die('Niet gelukt om verbinding te maken met de server: ' . mysql_error());
$db = mysql_select_db(DB_DATABASE); //Select database
if(!$db) die("Selecteren van DB mislukt");
in config.php i set the values for DB_host, DB_USER, DB_PASSWORD and DB_DATABASE
What am I doing wrong here?
You shouldn't use the mysql_* methods, as they have been deprecated since PHP 5.5. (A more thorough explanation of how to go from the mysql_* library to PDO).
To actually also answer your question, you probably don't have errors enabled on your website and don't see the error that occurs when the mysql connection fails (which stops execution of the PHP script at the line it occurs at)