Page 1 of 1
I updates my MYSQL db with a value using php.....
Posted: Mon Nov 14, 2011 11:06 pm
by abrahamrkj
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
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 12:46 am
by social_experiment
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
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 12:50 am
by abrahamrkj
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
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 1:08 am
by social_experiment
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?
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).
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';
}
?>
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.
Hth
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 1:19 am
by abrahamrkj
social_experiment wrote: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?
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).
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';
}
?>
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.
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
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 1:26 am
by abrahamrkj
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

Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 1:46 am
by social_experiment
abrahamrkj wrote:i want to update my same column again and again by a line feed
Ok, i thought you meant update...
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) . "') ";
?>
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 2:03 am
by abrahamrkj
social_experiment wrote:abrahamrkj wrote:i want to update my same column again and again by a line feed
Ok, i thought you meant update...
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) . "') ";
?>
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.
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?
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 2:32 am
by social_experiment
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
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
This is the basic process to achieve this
Hth
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 3:19 am
by abrahamrkj
social_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 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
This is the basic process to achieve this
Hth
based on ur pseudocode i made something but it shows one error....pls check this...THanks in advance
<?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';
}
}
?>
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 3:33 am
by social_experiment
Code: Select all
<?php
// $f = "SELECT haves FROM users WHERE name = '$sesname'; should be
$f = "SELECT haves FROM users WHERE name = '$sesname' ";
?>
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 4:29 am
by abrahamrkj
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
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 4:37 am
by social_experiment
abrahamrkj wrote:How to add line feed in db?
I don't quite understand what you have in mind

Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 6:11 am
by Celauran
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:
Code: Select all
+----+------+-------+
| id | name | haves |
+----+------+-------+
| 1 | ab | pizza |
+----+------+-------+
And you want it to be:
Code: Select all
+----+------+--------+
| id | name | haves |
+----+------+--------+
| 1 | ab | pizza |
| | | burger |
+----+------+--------+
Then you'll want a query similar to this:
Code: Select all
$sql = "UPDATE users SET haves = CONCAT(haves, '\n{$test}') WHERE name = '{$sesname}'";
Re: I updates my MYSQL db with a value using php.....
Posted: Tue Nov 15, 2011 6:19 am
by social_experiment
Interesting query; that would save the user from having to retrieve the data before updating it