Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
MYSQL update through PHP form
Old 03-15-2009, 07:28 AM MYSQL update through PHP form
Novice Talker

Posts: 5
Trades: 0
I want to use a php form textfield to change a value in my databas. I got this far by im keep getting this error, please help me figure out whats wrong.

PHP Code:
<?php

 $server 
"localhost";
  
$username "admin";
  
$password "admin";
  
$database "local_db";
  
$con mysql_connect($server$username$password);
  
$ok mysql_select_db($database$con);
      
        
$Cname mysql_real_escape_string($_POST['newname']);

    
$sql "UPDATE people SET name='{$Cname}' WHERE id=1";
    
mysql_query($sql) or die(mysql_error());

        

 
?>


<form method="post" action="">
  
    <input name="newname" type="text" id="textfield"/>
    
    <input type="submit" name="change" value="Change now" />
    
</form>
Notice: Undefined index: newname in this line,
PHP Code:
$Cname mysql_real_escape_string($_POST['newname']); 
Codeme is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-15-2009, 09:03 AM Re: MYSQL update through PHP form
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
it's probably only when you load this page without submitting it.
If you submit the form, the result should be ok, I don't see any errors in your code.

The fact is, that php will run through your update code at each time the page is loaded, not only when a post is made.
It's your job to tell php when to do the update.
PHP Code:
<?php

  $server 
"localhost";
  
$username "admin";
  
$password "admin";
  
$database "local_db";
  
$con mysql_connect($server$username$password);
  
$ok mysql_select_db($database$con);
  
  if(
sizeof($POST)>0){
    
//we go in here only where there was a POST submitted
    
$Cname mysql_real_escape_string($_POST['newname']);
    
$sql "UPDATE people SET name='{$Cname}' WHERE id=1";
    
mysql_query($sql) or die(mysql_error());
  }
?>
<form method="post" action="">
    <input name="newname" type="text" id="textfield"/>
    <input type="submit" name="change" value="Change now" />
</form>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-15-2009, 09:14 AM Re: MYSQL update through PHP form
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
You could also use:

PHP Code:
<?php

  $server 
"localhost";
  
$username "admin";
  
$password "admin";
  
$database "local_db";
  
$con mysql_connect($server$username$password);
  
mysql_select_db($database$con); // Don't actually need to make this a variable. 
  
  
if($_POST['change'] === 'Change now'){ // If the form has been submitted.
    
$sql "UPDATE people SET name='".mysql_real_escape_string($_POST['newname'])."' WHERE id=1 LIMIT 0,1"// Changed this line also.
    
mysql_query($sql) or die(mysql_error());
echo 
'Updated.';
  }
?>
<form method="post" action="">
    <input name="newname" type="text" id="textfield"/>
    <input type="submit" name="change" value="Change now" />
</form>
* Edit: Fixed that error.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 03-18-2009 at 06:17 AM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 03-15-2009, 06:33 PM Re: MYSQL update through PHP form
Novice Talker

Posts: 5
Trades: 0
Getting the following error from both above codes Notice: Undefined variable: POST

Last edited by Codeme; 03-15-2009 at 06:37 PM..
Codeme is offline
Reply With Quote
View Public Profile
 
Old 03-15-2009, 06:49 PM Re: MYSQL update through PHP form
chrishirst's Avatar
Defies a Status

Posts: 43,959
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
$_POST
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-16-2009, 12:38 AM Re: MYSQL update through PHP form
Jaryth000's Avatar
Skilled Talker

Posts: 59
Name: Jaryth
Location: Canada
Trades: 0
Just to confirm that, and so you know exactly where:

PHP Code:

<?php

  $server 
"localhost";
  
$username "admin";
  
$password "admin";
  
$database "local_db";
  
$con mysql_connect($server$username$password);
  
mysql_select_db($database$con); // Don't actually need to make this a variable. 
  
  
if($_POST['change'] === 'Change now'){ // If the form has been submitted.
    
$sql "UPDATE people SET name='".mysql_real_escape_string($_POST['newname'])."' WHERE id=1 LIMIT 0,1"// Changed this line also.
    
mysql_query($sql) or die(mysql_error());
echo 
'Updated.';
  }
?>
<form method="post" action="">
    <input name="newname" type="text" id="textfield"/>
    <input type="submit" name="change" value="Change now" />
</form>
</span>
__________________

Please login or register to view this content. Registration is FREE
My personal website
-Jaryth (UID590)
Jaryth000 is offline
Reply With Quote
View Public Profile Visit Jaryth000's homepage!
 
Old 03-18-2009, 04:36 AM Re: MYSQL update through PHP form
Skilled Talker

Posts: 54
Trades: 0
you can also use the following to execute the query only when the form is submitted
PHP Code:
<?php $server "localhost";
  
$username "admin";
  
$password "admin";
  
$database "local_db";
  
$con mysql_connect($server$username$password);
  
mysql_select_db($database$con); // Don't actually need to make this a variable. 
  
  
if($_POST){ // since there a single form that can be submitted
    
$sql "UPDATE people SET name='".mysql_real_escape_string($_POST['newname'])."' WHERE id=1 LIMIT 0,1"// Changed this line also.
    
mysql_query($sql) or die(mysql_error());
echo 
'Updated.';
  }
?>
<form method="post" action="">
    <input name="newname" type="text" id="textfield"/>
    <input type="submit" name="change" value="Change now" />
</form>
kani alavi is offline
Reply With Quote
View Public Profile
 
Old 04-04-2009, 04:03 PM SOLVED FOR ME !! . . MYSQL update through PHP form
BrianNY's Avatar
Junior Talker

Posts: 2
Name: Brian
Location: Saratoga Springs, NY
Trades: 0
Thank you guys!!

This site is great. After trying things for two days, Tripy's solution worked for me. Now I have to build on that to enable mysql updating from a forum with multiple records and a submit button.

Thanks so much .. this was the first time I could update the database from a form entry.
BrianNY is offline
Reply With Quote
View Public Profile
 
Old 04-04-2009, 07:22 PM Re: MYSQL update through PHP form
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Just a thught.
If this is the only action you take involving the database, that is if you don't do other things too with the database, you can also put this
PHP Code:
  $server "localhost";
  
$username "admin";
  
$password "admin";
  
$database "local_db";
  
$con mysql_connect($server$username$password);
  
$ok mysql_select_db($database$con); 
in the if statement. It's unnecessary to open a database connection if your not going to use it.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to MYSQL update through PHP form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25873 seconds with 11 queries