TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   UPDATE MySQL data via web forms? (http://www.talkphp.com/absolute-beginners/4243-update-mysql-data-via-web-forms.html)

Randy 05-06-2009 11:41 PM

UPDATE MySQL data via web forms?
 
So i am working on a CMS for a certain genre of websites, not much detail is needed but I am having some issues. I am trying to make it for that they can update the website details from a php page and for some reason it wont update the database..

Form/PHP:
PHP Code:

            <?php

                
if (isset($_POST['submit'])) {

                            
// Define Variables
                            
$websitename $_POST['websitename'];
                            
$tagline $_POST['tagline'];
                            
$websiteurl $_POST['websiteurl'];
                            
$contactemail $_POST['contactemail'];
                            
                        
// Attempt To Update
                        
$update mysql_query"UPDATE settings SET `sitename` = '$websitename' ") or die(mysql_error());
                            if(
mysql_num_rows($update) > 0
                                
{
                                        echo 
'SUCCESS';
                                }
                            else
                                {
                                        echo 
'FAILZOR';
                                }
                        
                } else {
            
?>
              <form action="" method="POST" id="settings" name="settings ">
                Website Name: <br />
                <label class="mediumInput">
                  <input name="websitename" id="websitename" type="text" size="75" />
                  </label>
                <br />
                <small>This will show up in the tab bar in your browser and all over the website.</small>
                <p>Tagline (Description): <br />
                  <label class="textareasmall">
                    <textarea name="tagline"  id="tagline" rows="2" cols="5"></textarea>
                  </label>
                <br />
                <small>This will show up on search engines under the name and will give a basic description of your website.</small></p>
                <p>Website URL: <br />
                  <label  class="mediumInput">
                    <input type="text" name="websiteurl" id="websiteurl" size="75"/>
                  </label>
                  <br />
                  <small>This is the URL to where you have this installed, It is recommend you do not change unless you know what you are doing.</small></p>
                <p>Contact Email: <br />
                  <label class="mediumInput">
                    <input name="contactemail" id="contactemail" type="text" size="75" />
                  </label>
                <br />
                <small>This is where emails will be sent to when contact form is used, as well as when emails are sent out.</small></p>
                <p>
            <input name="submit" id="submit" type="image" src="../images/bt_submit.gif" />
                  </p>
              </form>
              <?php
                
}
                
?>

I have updated the entire file here:
http://pastie.org/470648
PHP for Form starts on line: 170

Im not sure why its not updating, i know all my variables are right. I don't receive any errors at all, I hit submit and it just reloads the page and removes all entered text on the input

allworknoplay 05-06-2009 11:50 PM

Your query is missing a (...

$update = mysql_query"UPDATE settings SET `sitename` = '$websitename' ") or die(mysql_error());



$update = mysql_query("UPDATE settings SET `sitename` = '$websitename' ") or die(mysql_error());

Randy 05-06-2009 11:52 PM

even with that it doesn't work, i had it before but was trying new things. I added it back and tried still didnt work.

Village Idiot 05-06-2009 11:56 PM

You are not telling the database where to update, put a where clause at the end. Instead of modifying every row, mysql just doesn't do it (to do every row, do WHERE 1=1)

allworknoplay 05-06-2009 11:57 PM

Quote:

Originally Posted by Randy (Post 23749)
even with that it doesn't work, i had it before but was trying new things. I added it back and tried still didnt work.

Are you using IE or FF?

allworknoplay 05-06-2009 11:58 PM

Quote:

Originally Posted by Village Idiot (Post 23751)
You are not telling the database where to update, put a where clause at the end. Instead of modifying every row, mysql just doesn't do it (to do every row, do WHERE 1=1)

Good catch, that would be horrible to have all site names with the same name! Assuming that column is not unique....

Randy 05-07-2009 12:00 AM

column is 100% unique so, but I did that:

PHP Code:

$update mysql_query("UPDATE settings SET `sitename` = '$websitename' WHERE `build` = '1.0a'") or die(mysql_error()); 

and still nothing, i used build because its the "build" version of the script.

I am using FF3 I believe, not sure on exact version number as it is a work computer.

allworknoplay 05-07-2009 12:03 AM

Quote:

Originally Posted by Randy (Post 23754)
column is 100% unique so, but I did that:

PHP Code:

$update mysql_query("UPDATE settings SET `sitename` = '$websitename' WHERE `build` = '1.0a'") or die(mysql_error()); 

and still nothing, i used build because its the "build" version of the script.

I am using FF3 I believe, not sure on exact version number as it is a work computer.

Try this...


$update = mysql_query("UPDATE settings SET `sitename` = '".$websitename."' WHERE `build` = '1.0a'") or die(mysql_error());

Village Idiot 05-07-2009 12:05 AM

Another thing I noticed is that UPDATE does not return any rows, so your script will never show success. You are also missing a closing bracket on your if statement. Are you actually checking the values in the database or relying off of the script to say yes or no?

Randy 05-07-2009 12:07 AM

Did not work :( all I can think of is that perhaps its being blocked because i am running some lines at the top of the page to retrieve the data as its required for the general purpose of the website such as getting the title to display and other items.

Code At Top:
PHP Code:

$settingsquery mysql_query("SELECT * FROM settings") or die(mysql_error()); 
$setingsresult mysql_fetch_array$settingsquery ); 
$sitename $setingsresult['sitename'];
$siteurl $setingsresult['siteurl'];
$build $setingsresult['build']; 

that works just find just not this update issue.

Village Idiot 05-07-2009 12:08 AM

That shouldn't block it.

Can you access your database directly? Try running the query on phpmyadmin or whatever you have in your control panel.

allworknoplay 05-07-2009 12:11 AM

Just for kicks, take out your image button and try using a regular form button. I've ran into issues like this before with image buttons...


<input name="submit" id="submit" type="image" src="../images/bt_submit.gif" />


Try:

<input name="submit" id="submit" type="submit" />

Randy 05-07-2009 12:11 AM

It works if i run in it PHPMyAdmin just fine,

Im running this on XAMPP on my PC

Quick Note: I am heading out of my office now to go home and work on this some more, please give me about 30mins to an hour to expect a reply from me again,

Thanks for helping me with this, i have been trying to fix this all day and have gotten nowhere..

Randy 05-07-2009 12:12 AM

Quick Note On Allworknoplay's idea:

worked just getting this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\exotic\admin\settings.php on line 180

Village Idiot 05-07-2009 12:13 AM

Quote:

Originally Posted by Randy (Post 23762)
Thanks for helping me with this, i have been trying to fix this all day and have gotten nowhere..

I hope this is an issue regarding a project for work if that is the case :-)

Randy 05-07-2009 12:14 AM

no its not but i work as a server administrator i just sit here and make sure servers are working.

allworknoplay 05-07-2009 12:19 AM

Quote:

Originally Posted by Randy (Post 23763)
Quick Note On Allworknoplay's idea:

worked just getting this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\exotic\admin\settings.php on line 180

Give us your latest code because that is missing the ) too...

if(mysql_num_rows($update) > 0




if(mysql_num_rows($update) > 0 )

Randy 05-07-2009 12:49 AM

New Code:
PHP Code:

            <?php

                
if (isset($_POST['submit'])) {

                            
// Define Variables
                            
$websitename $_POST['websitename'];
                            
$tagline $_POST['tagline'];
                            
$websiteurl $_POST['websiteurl'];
                            
$contactemail $_POST['contactemail'];
                            
                        
// Attempt To Update
                        
$update mysql_query("UPDATE settings SET `sitename` = '".$websitename."' WHERE `build` = '1.0a'") or die(mysql_error());
                            if(
mysql_num_rows($update) > 0);
                        
                } else {
            
?>
              <form action="" method="POST" id="settings" name="settings ">
                Website Name: <br />
                <label class="mediumInput">
                  <input name="websitename" id="websitename" type="text" size="75" />
                  </label>
                <br />
                <small>This will show up in the tab bar in your browser and all over the website.</small>
                <p>Tagline (Description): <br />
                  <label class="textareasmall">
                    <textarea name="tagline"  id="tagline" rows="2" cols="5"></textarea>
                  </label>
                <br />
                <small>This will show up on search engines under the name and will give a basic description of your website.</small></p>
                <p>Website URL: <br />
                  <label  class="mediumInput">
                    <input type="text" name="websiteurl" id="websiteurl" size="75"/>
                  </label>
                  <br />
                  <small>This is the URL to where you have this installed, It is recommend you do not change unless you know what you are doing.</small></p>
                <p>Contact Email: <br />
                  <label class="mediumInput">
                    <input name="contactemail" id="contactemail" type="text" size="75" />
                  </label>
                <br />
                <small>This is where emails will be sent to when contact form is used, as well as when emails are sent out.</small></p>
                <p>
            <input name="submit" id="submit" type="submit" />
                  </p>
              </form>
              <?php
                
}
                
?>


Village Idiot 05-07-2009 12:52 AM

The UPDATE clause does not return any rows, counting the rows it returns will always be zero.

allworknoplay 05-07-2009 12:58 AM

Quote:

Originally Posted by Village Idiot (Post 23771)
The UPDATE clause does not return any rows, counting the rows it returns will always be zero.

Hmm.....can he do this?


PHP Code:

 $update mysql_query("UPDATE settings SET `sitename` = '".$websitename."' WHERE `build` = '1.0a'") or die(mysql_error());
if(
$update) {
echo 
"Success";




All times are GMT. The time now is 08:36 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0