I updates my MYSQL db with a value using php.....
Moderator: General Moderators
-
abrahamrkj
- Forum Newbie
- Posts: 14
- Joined: Mon Nov 14, 2011 10:59 pm
I updates my MYSQL db with a value using php.....
helo,
I am new to php, I am doing one project in which i UPDATED MY MYSQL DB say( table name --> content1) but after tht i have to add another data on that same table and same column after previous data....How to do it????
Thanks in advance
I am new to php, I am doing one project in which i UPDATED MY MYSQL DB say( table name --> content1) but after tht i have to add another data on that same table and same column after previous data....How to do it????
Thanks in advance
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: I updates my MYSQL db with a value using php.....
I had written out a little explanation but now i'm not sure i understand your question; Do you want to update the same column each time? Your existing code should be sufficient
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
abrahamrkj
- Forum Newbie
- Posts: 14
- Joined: Mon Nov 14, 2011 10:59 pm
Re: I updates my MYSQL db with a value using php.....
ss....i want to update my same column again and again by a line feed....for that what specification in database say(varchar,text or etc) and how to do it?
<?php
$test = $_SESSION['title'];
echo $test,$sesname;
$con = mysql_connect("localhost","root","abraham");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("UPDATE users SET haves = '$test'
WHERE name = '$sesname'");
echo "added";
?>
and i want to update again and again
<?php
$test = $_SESSION['title'];
echo $test,$sesname;
$con = mysql_connect("localhost","root","abraham");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("UPDATE users SET haves = '$test'
WHERE name = '$sesname'");
echo "added";
?>
and i want to update again and again
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: I updates my MYSQL db with a value using php.....
If you will do this yourself, your script looks as if it will do the job; if you want it automated, look at using a cron job. I updated your query to include a security measure, escape data that you enter into your database (or any data you receive from an external source, or for that matter, any data you enter into the database).abrahamrkj wrote:ss....i want to update my same column again and again by a line feed....for that what specification in database say(varchar,text or etc) and how to do it?
Code: Select all
<?php
$con = mysql_connect("localhost","root","abraham");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = "UPDATE users SET haves = '" . mysql_real_escape_string($test) . "' where
name = '" . mysql_real_escape_string($sesname) . "' ";
$qry = mysql_query($sql);
if ($qry) {
echo 'added';
}
?>Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
abrahamrkj
- Forum Newbie
- Posts: 14
- Joined: Mon Nov 14, 2011 10:59 pm
Re: I updates my MYSQL db with a value using php.....
social_experiment wrote:If you will do this yourself, your script looks as if it will do the job; if you want it automated, look at using a cron job. I updated your query to include a security measure, escape data that you enter into your database (or any data you receive from an external source, or for that matter, any data you enter into the database).abrahamrkj wrote:ss....i want to update my same column again and again by a line feed....for that what specification in database say(varchar,text or etc) and how to do it?
The above example includes escaping the data you receive / use. It also will only echo 'added' if the query is successful. Hint: Use PHP Code button to wrap your code in: reads easier and is good forum manners imo.Code: Select all
<?php $con = mysql_connect("localhost","root","abraham"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $sql = "UPDATE users SET haves = '" . mysql_real_escape_string($test) . "' where name = '" . mysql_real_escape_string($sesname) . "' "; $qry = mysql_query($sql); if ($qry) { echo 'added'; } ?>
Hth
Thank u so much......but i am getting a "undefined inde:..." on $test = $_SESSION['title'];....pls i had posted it on another post pls solve it.... Thank u viewtopic.php?f=1&t=132795
-
abrahamrkj
- Forum Newbie
- Posts: 14
- Joined: Mon Nov 14, 2011 10:59 pm
Re: I updates my MYSQL db with a value using php.....
This code is replacing my previous data in DB (i.e)haves column but i want to add with the old one with line feed 
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: I updates my MYSQL db with a value using php.....
Ok, i thought you meant update...abrahamrkj wrote:i want to update my same column again and again by a line feed
The SQL query has to be an INSERT one to create a new row each time.
Code: Select all
<?php
// change your update query to this
$sql = "INSERT INTO users (haves, name) VALUES ('" . mysql_real_escape_string($test) . "', '" . mysql_real_escape_string($sesname) . "') ";
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
abrahamrkj
- Forum Newbie
- Posts: 14
- Joined: Mon Nov 14, 2011 10:59 pm
Re: I updates my MYSQL db with a value using php.....
Actually I want to ADD DATAi(.e)'$test' to db for the same user who is already created during registration......After i update by checking with $sesname i want to add again after the previous data.social_experiment wrote:Ok, i thought you meant update...abrahamrkj wrote:i want to update my same column again and again by a line feed
The SQL query has to be an INSERT one to create a new row each time.Code: Select all
<?php // change your update query to this $sql = "INSERT INTO users (haves, name) VALUES ('" . mysql_real_escape_string($test) . "', '" . mysql_real_escape_string($sesname) . "') "; ?>
For ex:
updating
if id | name($sesname) | haves($test)
1.| ab | pizza
after that i have to add "burger" to the SAME USER...
id | name($sesname) | haves($test)
1.| ab | pizza
............................... burger
How to do it...pls help?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: I updates my MYSQL db with a value using php.....
For that you will have to retrieve the previously entered data or it will be overwritten.
Here is some psuedo-code of what you need to script
This is the basic process to achieve this
Hth
Here is some psuedo-code of what you need to script
Code: Select all
// count the amount of rows for your user
$result = SELECT COUNT(id) FROM table WHERE username = $user
if ($result == 1)
// this user has already entered data, now retrieve his existing
// data
SELECT data FROM table WHERE username = $user
// use mysql_fetch_array (for example) to retrieve data and
// assign it to a variable
$data = $array['data']
// new data is now added to existing set
$newData = $data . ', ' . $test
// write new info to the database
UPDATE table SET haves = $newData WHERE username = $user
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
abrahamrkj
- Forum Newbie
- Posts: 14
- Joined: Mon Nov 14, 2011 10:59 pm
Re: I updates my MYSQL db with a value using php.....
based on ur pseudocode i made something but it shows one error....pls check this...THanks in advancesocial_experiment wrote:For that you will have to retrieve the previously entered data or it will be overwritten.
Here is some psuedo-code of what you need to scriptThis is the basic process to achieve thisCode: Select all
// count the amount of rows for your user $result = SELECT COUNT(id) FROM table WHERE username = $user if ($result == 1) // this user has already entered data, now retrieve his existing // data SELECT data FROM table WHERE username = $user // use mysql_fetch_array (for example) to retrieve data and // assign it to a variable $data = $array['data'] // new data is now added to existing set $newData = $data . ', ' . $test // write new info to the database UPDATE table SET haves = $newData WHERE username = $user
Hth
<?php
$con = mysql_connect("localhost","root","abraham");
$test = (isset($_SESSION['title'])) ? $title = $_SESSION['title'] : $title = $_SESSION['title1'];
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT COUNT(haves) FROM users WHERE name = '$sesname'");
if ($result == 1)
{
$f = "SELECT haves FROM users WHERE name = '$sesname';
$f1 = mysql_query($f);
$array = mysql_fetch_array($f1); // syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE
$data = $array['haves'];
$newData = $data . '\n' . $test;
$sql = "UPDATE users SET haves = '" . mysql_real_escape_string($test) . "' where name = '" . mysql_real_escape_string($sesname) . "' ";
$qry = mysql_query($sql);
if ($qry)
{
echo 'added';
}
}
else
{
$sql = "UPDATE users SET haves = '" . mysql_real_escape_string($test) . "' where name = '" . mysql_real_escape_string($sesname) . "' ";
$qry = mysql_query($sql);
if ($qry) {
echo 'added';
}
}
?>
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: I updates my MYSQL db with a value using php.....
Code: Select all
<?php
// $f = "SELECT haves FROM users WHERE name = '$sesname'; should be
$f = "SELECT haves FROM users WHERE name = '$sesname' ";
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
abrahamrkj
- Forum Newbie
- Posts: 14
- Joined: Mon Nov 14, 2011 10:59 pm
Re: I updates my MYSQL db with a value using php.....
social_experiment wrote:Code: Select all
<?php // $f = "SELECT haves FROM users WHERE name = '$sesname'; should be $f = "SELECT haves FROM users WHERE name = '$sesname' "; ?>
Finally i got it and worked....but it is storing in a single line (pizza,burger....etc) ...How to add line feed in db? Thank u so much
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: I updates my MYSQL db with a value using php.....
I don't quite understand what you have in mindabrahamrkj wrote:How to add line feed in db?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: I updates my MYSQL db with a value using php.....
Using your previous example, if you want haves to contain 'pizza' and 'burger' in the same column and row, you'll need to add a line break before each additional item.
So if the current row is:
And you want it to be:
Then you'll want a query similar to this:
So if the current row is:
Code: Select all
+----+------+-------+
| id | name | haves |
+----+------+-------+
| 1 | ab | pizza |
+----+------+-------+Code: Select all
+----+------+--------+
| id | name | haves |
+----+------+--------+
| 1 | ab | pizza |
| | | burger |
+----+------+--------+Code: Select all
$sql = "UPDATE users SET haves = CONCAT(haves, '\n{$test}') WHERE name = '{$sesname}'";- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: I updates my MYSQL db with a value using php.....
Interesting query; that would save the user from having to retrieve the data before updating it
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering