[PHP/SQL] Editing SQL Values

Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Hello, I am making a website for a friend, and he is not very website literate, so I am in the middle of converting all of the content into a CMS based environment so he can edit the content easily.

I have managed to create my database, tables, columns etc and integrate it into the website, now I need to make the housekeeping side of it.

What i need to achieve is a form that can be used to EDIT the current values

here's an example:

edit the columns 'home_header' and 'home_content' in the table 'site_home'

This is my first SQL project and so far I have managed to setup the rest on my own, but I now require your help.

Thanks in advance for ANY help whatsoever.

Curtis
 
Joined
Sep 10, 2006
Messages
2,812
Reaction score
1,416
Re: Editing SQL Values

PHP:
<?php
// here somehow connect to sql 

$query = "SELECT * FROM site_home";
$result = mysql_query($query);
$array = mysql_fetch_array($result);

if(isset($_POST['submit']))
{
   $query2 = "UPDATE site_home SET home_header =  $_POST['header']";
   $result2 = mysql_query($query);
}
?>
<form action="" method="POST">
<textarea name="header"><?php $array['home_header']; ?></textarea><?php if($result){ echo "updated"; } ?> 
<input type="submit" name="submit">
</form>
 
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

Thanks for the quick response. I have hooked it up to my SQL db, but i get this error :(

Code:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\football\housekeeping\home.php on line 10

edit: this is the code i am currently using:

PHP:
<?php
include('../config.php');
 
$query = "SELECT * FROM site_home";
$result = mysql_query($query);
$array = mysql_fetch_array($result);
 
if(isset($_POST['header']))
{
   $query2 = "UPDATE site_home SET home_header =  $_POST['header']";
   $result2 = mysql_query($query);
}
?>
<form arction="" method="POST">
<textarea name="header"><?php $array['home_header']; ?></textarea><?php if($result){ echo "updated"; } ?> 
<input type="submit">
</form>
 
Joined
Sep 10, 2006
Messages
2,812
Reaction score
1,416
Re: Editing SQL Values

expectable, I've written it just from my mind to this quick reply box, without paying much attention..
Anyway,
PHP:
<?php
include('../config.php');
 
$query = "SELECT * FROM site_home";
$result = mysql_query($query);
$array = mysql_fetch_array($result);
$homeheader = $_POST['header'];

if(isset($_POST['submit']))
{
   $query2 = "UPDATE `site_home` SET `home_header` = '$homeheader'";
   $result2 = mysql_query($query2);
}
?>
<form action="" method="POST">
<textarea name="header"><?php $array['home_header']; ?></textarea><?php if($result2){ echo "updated"; } ?>
<input type="submit">
</form>
 
Last edited:
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

It still isnt working. argh.

PHP:
<?php
include('../config.php');
 
$query = "SELECT * FROM site_home";
$result = mysql_query($query);
$array = mysql_fetch_array($result);
$homeheader = $_POST['home_header'];
if(isset($_POST['header']))
{
   $query2 = "UPDATE site_home SET home_header =  $homeheader";
   $result2 = mysql_query($query);
}
?>
<form arction="" method="POST">
<textarea name="header"><?php $array['home_header']; ?></textarea><?php if($result){ echo "updated"; } ?>
<input type="submit">
</form>

I also had to add a semicolon onto the end of:
PHP:
$homeheader = $_POST['home_header']

It now comes up with the form but it's not working as it should

screenie:
CurtisK - [PHP/SQL] Editing SQL Values - RaGEZONE Forums


when i type something and press enter, the changes are not made.



Thank you so much for your help so far by the way :)
 
Joined
Sep 10, 2006
Messages
2,812
Reaction score
1,416
Re: Editing SQL Values

Yea, reading my posts directly after I post them = not good(I've edited it like 7x) :p last edited version should be kinda working though. Anyway I'm off, it's late night alreay, I'm sure you'll figure it out somehow..
 
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

Not working at all :(

i type something in the box and submit, but it does not update the database.
 
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

Well that changed things. I now get this error when i submit the form

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
 
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

I hadn't noticed you'd edited that. Sorry.

Well anyway i have replaced the variables and stuff in later posts, but it still does the same thing as before

CurtisK - [PHP/SQL] Editing SQL Values - RaGEZONE Forums


no error message whatsoever. I press submit and the form is cleared and no changes are made to the DB

PHP:
<?php
include('../config.php');
 
$query = "SELECT * FROM site_home";
$result = mysql_query($query) or die(mysql_error());
$array = mysql_fetch_array($result);
$homeheader = $_POST['header'];

if(isset($_POST['submit']))
{
   $query2 = "UPDATE `site_home` SET `home_header` = '$homeheader'";
   $result2 = mysql_query($query2) or die(mysql_error());
}
?>
<form action="" method="POST">
<textarea name="header"><?php $array['home_header']; ?></textarea><?php if($result){ echo "updated"; } ?>
<input type="submit">
</form>
 
Last edited:
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

Yes. There is currently an entry called 'Homepage'
 
Joined
Sep 10, 2006
Messages
2,812
Reaction score
1,416
Re: Editing SQL Values

1st notice missing echo in that textarea, so it should be
<textarea name="header"><?php echo $array['home_header']; ?></textarea><?php if($result2){ echo "updated"; } ?>
2nd dunno yet, wait :p
 
Joined
Sep 10, 2006
Messages
2,812
Reaction score
1,416
Re: Editing SQL Values

This should definitely work, problem was with that isset()
PHP:
<?php
mysql_connect(localhost, ales, ******);
mysql_select_db(homopelo);
$query = "SELECT * FROM site_home";
$result = mysql_query($query);
$array = mysql_fetch_array($result) or die(mysql_error());
$homeheader = $_POST['header'];
if(isset($_POST['header']))
{
   $query2 = "UPDATE `site_home` SET `home_header` = '$homeheader'";
   $result2 = mysql_query($query2) or die(mysql_error());
   //to refresh
   header('refresh: 2, testik.php');
}
?>
<form action="" method="POST">
<textarea name="header">
<?php
 echo $array['home_header'];
 ?>
 
</textarea>
<?php if($result2){ echo "updated"; } ?>
<br />
<input type="submit">
</form>
 
Last edited:
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

Okay, we are making progress (slowly)

Here's the php code with everything of mine entered correctly:

PHP:
<?php
mysql_connect(localhost, root, *******);
mysql_select_db(site_home);
$query = "SELECT * FROM site_home";
$result = mysql_query($query);
$array = mysql_fetch_array($result) or die(mysql_error());
$homeheader = $_POST['header'];
if(isset($_POST['header']))
{
   $query2 = "UPDATE `site_home` SET `home_header` = '$homeheader'";
   $result2 = mysql_query($query2) or die(mysql_error());
   //to refresh
   header('location: testik.php');
}
?>
<form action="" method="POST">
<textarea name="header">
<?php
 echo $array['home_header'];
 ?>
 
</textarea>
<?php if($result2){ echo "updated"; } ?>
<br />
<input type="submit">
</form>

the error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\football\housekeeping\home.php on line 6
No database selected
 
Joined
Sep 10, 2006
Messages
2,812
Reaction score
1,416
Re: Editing SQL Values

mysql_select_db(site_home);
are you sure that it's a dabatase name? Not a column? ;) You could just delete those lines and use your include imo ;)

Also change
header('location: testik.php');
to
header('refresh: 2, home.php');

so
PHP:
<?php
include('../config.php'); 

$query = "SELECT * FROM site_home";
$result = mysql_query($query);
$array = mysql_fetch_array($result) or die(mysql_error());
$homecontent = $_POST['content'];
$homeheader = $_POST['header'];
if(isset($_POST['content']))
{
   $query2 = "UPDATE `site_home` SET `home_content` = '$homecontent ', `home_header` = '$homeheader'";
   $result2 = mysql_query($query2) or die(mysql_error());
   //to refresh
   header('refresh: 2, home.php');
}
?>
<form action="" method="POST">
<p>header</p>
<input type="text" name ="header" value="<?php  echo $array['home_header']; ?>" />
<br />
<p>content</p>
<textarea name="content">
<?php
 echo $array['home_content'];
 ?>
 
</textarea>
<?php if($result2){ echo "updated"; } ?>
<br />
<input type="submit">
</form>
 
Last edited:
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

AHA thanks :)

i used your revised version above. And yes i did get the database name wrong (shamefully :( )

It's working!

Thanks so much for your help!
I owe you big time...
 
Newbie Spellweaver
Joined
Sep 13, 2007
Messages
16
Reaction score
0
Re: Editing SQL Values

Brilliant! Thank you so so so much. I think i am going to take the same route as you. Bed time!

I will get the mysql/php integration sometime. I'm just not that experienced yet.

Once again thank you.


EDIT: If it's okay with the site owner, I will most definately put some form of credit somewhere on the site.
 
Back
Top