PHP & MySQL - Updating Query - php

I need some help with updating query (html, php, mysql).
So I tried updating mySQL query (signature) connected with HTML. But I often face some problems.
I'd like to update a query (signature) by typing new signature in a form and a button that will submit action and change.
So basically I want to update user's signature when he types new one in a field and submits (presses a button).
If anyone can make a basic system for me so we will look forward successfully making the system.
Edit:
Yeah I know that nobody will write the script for me but I'm just out of ideas how could it be made. I'm no professional at all. I apologize for that :)
Anyway, this is the code:
<form action="update_signature.php">
<input type="text" name="txt" />
<input type="submit" name="insert" value="insert" onclick="insert()" />
</form>
I'm not sure what should I type in update_signature.php so I wrote a simple query:
<?php
$myq = mysql_query("UPDATE userSignature FROM users WHERE userSignature='$signature'")
$row=mysql_fetch_array($myq);
?> `
Thanks :)

I like how StackOverflow does not allow comments under 50 reputation...
The HTML code should have a "method", but if you skip it, it just posts as a "get", so not really a problem. In the "onclick" parameter however, you give it a JavaScript function, which does not exist?
The PHP is a mess. First, you need to retrieve the sent value with
$something = $_GET['txt'];
This puts the value what was left in the input with the name txt into a variable.
Second, the UPDATE syntax is totally different, it's more like
"UPDATE table SET column = '$phpvariable', etc... WHERE column2 = '$phpvariable2'"
where column is the name of the column in your table, and the query is successful in the lines where the column2 value is equal to the $phpvariable2.
Third, the UPDATE returns with a yes or no (success or not), not with an array.

Related

PHP insert data into SQL Database Table produces blank row

I'm trying to insert data from the form created into the SQL Server Database Table that is connected through ODBC. After I submit the data it shows up as a blank row with only the ID that has a value. I am very new to PHP and got parts of code from tutorials. This is just a test project. Here's the code:
<html>
<header>
<title>
</title>
</header>
<body>
<form action="\INSERTCODE.php" method="POST">
<input type= "number" step="any" name="sepal_lengp" placeholder="Sepal Length">
<input type= "number" step="any" name="sepal_widthp" placeholder="Sepal Width">
<input type= "number" step="any" name="petal_lengp" placeholder="Petal Length">
<input type= "number" step="any" name="petal_widthp" placeholder="Petal Width">
<input type= "text" name="flower_type" placeholder="Flower Name">
<button type="submit" name="submit" >INPUT VALUES</button>
</form>
<?php
//display all results from table
include("C:\Users\Dshop\Desktop\php-7.3.3\Server1\connection.php");
$i=1;
$sql = "SELECT * FROM dbo.textcsv";
$result = odbc_exec( $connection, $sql );
while($all =odbc_result_all($result,$i) ){
echo $all;
}
?>
</body>
</html>
This part includes the form. The filename is index1.php.
<?php
include("C:\Users\Dshop\Desktop\php-7.3.3\Server1\connection.php");
$sepal_lengp = $_POST['sepal_lengp']??'';
$sepal_widthp = $_POST['sepal_widthp']??'';
$petal_lengp = $_POST['petal_lengp']??'';
$petal_widthp = $_POST['petal_widthp']??'';
$flower_typep = $_POST['flower_typep']??'';
$dbinsert = "INSERT INTO dbo.textcsv (sepal_leng, sepal_width, petal_leng, petal_width, flower_type) VALUES ('$sepal_lengp', '$sepal_widthp', '$petal_lengp', '$petal_widthp', '$flower_typep');";
odbc_exec( $connection, $dbinsert );
HEADER("Location: ../index1.php?=success");
This part inserts data into the database table, using $_POST to obtain the data from index1.php. This file is called INSERTCODE.php. $connection and connection.php is the file that includes the connection to ODBC.
For this test project I used the Iris dataset. I believe that I had to use ODBC and SQL Server instead of mysql. Sql server is the 2014 version, PHP is 7.33, using node.js to run the server. Help is greatly appreciated!
EDIT I found out that the $_POST isn't getting any values from the form. Any ideas?
EDIT 2 I've tried using $_REQUEST, checking var_dump, and did all that stuff, but I still got nothing. After going to https://www.w3schools.com/php7/php7_forms.asp for an example form, I found out that the example did not work either. Now i'm not sure if the problem is from the code, or from something like the php configuration. Need help, please help.
you're treating your variables as strings, by th look of your database you want them as floats. Try using floatval( ) (http://php.net/manual/en/function.floatval.php) on your variables to make sure they are in the write format, this will also go some way to sanitising them until you update this to prepare statements so you can safely bind the values and specify the type
After rephrasing my issue to "node.js not taking post data" I found the issue. Node.js needs extra steps to process POST data. So, because of this, my input was ignored by node.js and the INSERTDATA.php ran without any data to insert anything. Turns out the solution of the problem was to use something like the body-parser or the other solution from another question.
The solution I took was to uninstall node.js and use XAMPP instead. It was much easier to use.
Also could someone flag my question for duplicate?

How to get the right forms value from a button click

Overview of what my question is:
I have an array that is populated via XML inputs, and from this I am the using it to populate a web form with form controls. From here I want to be able to select the exact form that is clicked, but to do that I need to give the controls some form of unique identifier, which is an issue...
As the site is of a betting nature and I am currently working with horse racing, each horse is given a unique identifier by default, I have tried to add this identifier to the forms.
e.g:
<?php
//Values from feed examples: 123, 234, 345
$valuesFromFeed = array(123, 234, 345); //These are not in my code, they are values from the XML feed
while ($uniqueIdentifier = $valueFromFeed) {
<form name="horse_<?php echo $uniqueIdentifier; ?>_frm" action="#">
<input type="hidden" name="horse_<?php echo $uniqueIdentifier; ?>" />
<input type="button" value="Place bet" />
</form>
}
?>
But then the problem comes when I try to reference this name of "horse_123", I need to know exactly what the value of that name is, which is impossible as there are millions of horses, tracks and races.
Example of trying to get post:
<?php
if (isset($_POST['horse_' . $uniqueIdetifier])) {
echo "You got the right thing here.";
} else {
echo "Still no joy.";
}
?>
The issue with the code above, is that once the $uniqueIdentifier has been used in the while above, it is removed and is no longer usable in this scope.
So to conclude, my point and question:
How do I get the correct name from the form in a submit for the specific horse that I wish to reference and get information on?
How do I use this information as I need to?
Better Description:
I have been given an XML feed and site as part of a handover, this feed contains many hundreds of races and horses.
When this information is loaded into the page, it is also stored in a database on the server, as well as sending it through some different loops (which are messy, but someone else's code I'm trying to clean up!) which split it down and then make up a dynamic menu containing all the races, horses, odds and information. (All information on a single horse within a race is kept in one form)
Next to the information stated in the prior paragraph, is 2 buttons, one that allows the user to take odds and another that allows users to take starting price.
On either of these button clicks, I need the information attached to said horse, and then populate a betting slip. In the form (mentioned above) the name is "horse_<?php echo $uniqueIdentifier; ?>_frm".
The problem that occurs to me is, yes data is stored on the server when it is loaded, that I cannot seem to get the right form via the unique identifier that is put into the form name
Edits
Added form surrounding my input as this is there, I just missed it in original question
Added the button that transmits data to where I need it
Added a better description of my problem
You can use multiple forms, one for each horse. Each form has a different action, where the URL includes the id of the horse. For example:
<form action="/horses/my_unique_horse_name">
...
</form>
<form action="/horses/another_horse_name">
...
</form>
Or you could have multiple forms all with the same action, with a hidden field for the name of the horse:
<form action="/horses/">
<input type="hidden" value="my_unique_horse_name">
</form>
<form action="/horses/">
<input type="hidden" value="another_horse_name">
</form>
Alternatively, you could have a button for each horse:
<form method="/horses/">
<button type="submit" value="my_unique_horse_name">My Horse</button>
<button type="submit" value="another_horse_name">Another Horse</button>
</form>
Beyond that, I don't entirely understand the problem. What kind of data are you submitting and retrieving?

PHP 'Checkbox' Alternative when updating

I have a small problem with my PHP Code and as always, any help is hugely appreciated.
The code was originally designed by another person and I seem to have come across a problem that I can't fix.
The structure of the page is as follows:
A while loop to output the data, each of course, has a unique id.
A checkbox that must be ticked before pressing the 'update' button
(several update buttons present pending on how many rows are
returned.
This is what i'm confused on, he has designed it to they must tick the 'checkbox' before pressing update, otherwise, the script doesn't know which 'id' to update. Why is it like this?!
<?php
if($_POST['accept']){
if(!$_POST['check']){
echo "<div class='error_input'>Error! please tick box to confirm </div>";
}else{
$form_id=$_POST['fid'];
$customer=$_POST['customer'];
mysql_query("UPDATE job SET customer='$customer'WHERE f_id=$form_id ");
echo "<div class='form_ok'>Job has been updated.</div>";
}
?>
If I just comment out the check box, and the user clicks the 'accept' button, it doesn't work, so clearly the 'checkbox' is pointing to the unique ID of the row, but I can't see it?
<input type="checkbox" name="check" value="1" required/>
<input type="submit" name="accept" value="Accept Job">
My issue is, I want to be able to remove this annoying checkbox, and once the user has pressed 'accept' add the unique ID into a session, which I know can be done by:
$_SESSION['user_id'] = $user_id;
As they then get redirected to a pop-up box to which I need this unique ID to pull further data from the database.
I've had a go at implemented this:
<input type="submit" name="accept" value="<?php echo"$user_id"; ?>" class="sbutton">
which works in a nutshell, how ever, it's not user friendly and I can't access the user_id, when I use the $_POST feature, it saves the 'name' field.
I know this is long winded, I hope i've given you enough information, many thanks in advance.
It seems like there is another hidden field in there with the job ID, and each row has it's own <form> tags - so if you'll remove the check for the check field than you're fine.
Like that:
<?php
if($_POST['accept'])
{
$form_id=$_POST['fid'];
$customer=$_POST['customer'];
mysql_query("UPDATE job SET customer='".$customer."' WHERE f_id=".$form_id);
echo "<div class='form_ok'>Job has been updated.</div>";
}
?>
By the way - it's best that you'll check and secure the values from the $_POST with check for numbers and securing against SQL Injection with mysql_real_escape_string function

Very quick, easy, webpage with increment/decrement

I'm looking for any direction on a really quick and dirty webpage. I'm going to have two static items, say person A and person B. I would like to click a (+) or (-) button next to each of them which then increments or decrements an integer that's displayed relative to each person.
Anyone having a quick tut or anything would be useful.
Aside from this, how hard would it be to keep a viewable log of each time the value was altered either incremented or decremented, would it be easy to add in date/time to that?
Edit
Alright, concerning mysql. I have a db already setup from a previous wordpress installation. I'm going to create a new tables named 'points', should this have 2 fields? One for a person A and one for a person B?
Since you want it to remember the value between sessions (ie- if I incremented the counter, left the website, and came back, I expect it to still be incremented) you need to store the value server-side. Databases are the best recommendation for this.
If you're planning to use PHP (which I assume from the tags), then MySQL is one of the easiest databases to implement. If you already have a MySQL database set up at your host, then this will be easy. If not, how to set up a MySQL database will be another question you need to ask.
Since you want quick and dirty the best method would be a form. Either POST or GET (preferably GET if you want people to send a "vote up this image" link, preferably POST if you don't want such links to be possible). This is easy, but it also requires reloading the page which is why modern voting systems use AJAX calls (javascript).
Your HTML form would look something like this:
<!-- Person A goes here -->
This person has a score of <!-- We'll do this soon -->.
<form method="get" <!-- or post --> action="vote.php">
<input type="submit" name="submit_button" value="Vote Up!" />
<input type="submit" name="submit_button" value="Vote Down =(" />
<input type="hidden" name="person" value="A" />
</form>
<!-- Similar for person B -->
Note that <!-- --> is the syntax for an HTML comment (these will be removed in the final website)
In vote.php you would need to first see if the form was submitted, then see WHICH submit button was pressed (vote up or down), then see which person it applies to. Then we do our database entry.
<?php
if(isset($_GET['submit_button'])){
// They submit the form
$add = ($_GET['submit_button'] == 'Vote Up!') ? 1 : -1;
$person = $_GET['person'];
$link = mysql_connect('localhost', 'user', 'password');
mysql_select_db('database', $link);
mysql_query("UPDATE table SET value=value+$add WHERE person=$person");
} else {
die("You didn't submit the form =(");
}
?>
Mind you this is a REALLY dirty method (there is no parsing of the query and no checks made. This is very susceptible to an SQL injection. Do NOT use this in a database with important information. In fact- probably don't use this at all without a few changes =) )
Now then, this basically takes the table table, finds the entry where person equals whatever person was selected (chosen by which form was used to submit), then adds either +1 or -1 to value. You can change any of these variable names in your own table. The next step: reading the value to display on the previous page. Remember that before I just had the comment <!-- we'll do this soon -->. We'll get to that now.
In the beginning of your first page you want to read the database. This means your first page must also be PHP.
<?php
$link = mysql_connect('localhost', 'user', 'password');
mysql_select_db('database', $link);
$result = mysql_query("SELECT value FROM table WHERE person=A");
...
Now you have a MySQL resource, but you need the value out of it.
...
$row = mysql_fetch_row($result);
...
And now we display it with the information.
<!-- Person A goes here -->
This person has a score of <?php echo $row[0]; ?>.
<form method="get" <!-- or post --> action="vote.php">
<input type="submit" name="submit_button" value="Vote Up!" />
<input type="submit" name="submit_button" value="Vote Down =(" />
<input type="hidden" name="person" value="A" />
</form>
Then you repeat for person B. This is really the dirtiest method since it involves one call to the database per person. Ideally you'd grab all values you want in a single call and then iterate over the returned resource and determine who was who. Or, if you really wanted to be fancy, you could already know who was who by using SORT ASC =)
Like I said, though, this is the quickest, easiest, and dirtiest method to do what you want using PHP and MySQL.
Anyway I'd go for the database storing of person data and increment by id .
This is the flow for it :
Create mysql or mysqli database "person", create table "rates" id - autoincrement
rate will be integer.
Create php file with database connection and the functions related to it.
you can use same file to create increment function, when row is the result from the integer relative to this entry :
$rate = $row['rate'];
function incvar(){
global $rate;
if(isset($rate) || $rate>0){
return $rate++;
}
}
function decvar(){
global $rate;
if(isset($rate) || $rate>0){
return $rate--;
}
}
Now create query updating existing fields with results from those functions.
Ideally is to create ajax request onclick to this file to increase and decrease the integer. For fast execution use jquery.

Input questions mysql php html

(Q1)Hi I'm using textbox in my project and I can't receive the values that are typed
<textarea rows="5" cols="60"> Type your suggestion </textarea>
<br>
<input type="submit" name="sugestao" value="Submit" />
Sorry I don't know how to 'kill' html code, that's why < is missing.
All I'm getting in the column of the database from this text box is "Submit", I'd like to receive whatever is written in the text area. How can I make the value equal whaterever is typed?
(Q2) How can I make sure that I'll only store the same type(int,varchar,text) that I setted,declared in the database. For example: age(int), but if someone types "abc" in the input it will be stored in my database as the value 0 . How can I forbid this, and only save the age when it's just int and all the other fields(like name, email) are filled ?. And if is still possible warn the user that he is typing something wrong, don't need to say where.
Sorry for any mistake in English and Thanks for the attention.
You need a name attribute for your textarea, otherwise, when you submit the form, the text you have entered is not sent.
<form method="post" action="your_handler.php">
<textarea rows="5" cols="60" name="question">Type your suggestion</textarea>
<input type="submit" name="sugestao" value="Submit" />
</form>
For your second question, you can implement input validation on client side (with javascript), server side (with whatever language you are using), or both.
For age validation as an example you want to validate two things:
You have a number
It's equal to, or greater than say 16
With PHP you check this way:
if( (int) $_POST['age'] > 15) {
// insert the record
} else {
// display error message
}
I think that you are missing the name="" attribute in your textarea. If you add it, you will receive its value in the $_POST array. For eaxmple, name="abc" will result in $_POST['abc']
For the second question, you need to do form validation. Look for this in Google, it is a basic task.
If you want to check for data types before saving, you can use PHP's built in methods to do so. There are lots of methods for this with php - is_numeric(), is_string(), is_float() and so forth. When the data is passed to the server, the POST data will initially be in string form, so you may have to cast it ($castVar = (int)$var) before you run any of these methods.
q2. you can't do it automatically but it can be done manually.
Just add a validation code in the form handler script.
if ($_POST['age'] < 13 or $_POST['age'] > 110) $err[] = "Wrong age value!";

Categories