Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Edit Database

Featured Replies

Okay so I can add to my database using a form, but now I'd like to do it so whatever you type into the form will edit fields already in the database, heres my code;

 

<?php

define('DB_NAME', 'TourneyLeague');
define('DB_USER', 'root');
define('DB_PASSWORD', 'pw');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$value = $_POST['Name'];
$value2 = $_POST['Points'];

$sql = "INSERT INTO tourneytable (Name, Points) VALUES ('$value', '$value2')"; 

if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}

mysql_close();
?>

 

 

I understand Ill have to use there WHERE tag on this line, or maybe the UPDATE tag??

 

$sql = "INSERT INTO tourneytable (Name, Points) VALUES ('$value', '$value2')"; 

 

I just dont know how to go about it, help please! thanks

Edited by Willows

What do you mean with "but now I'd like to do it so whatever you type into the form will edit fields already in the database" ?

 

Do you mean that if the data given in the form already exist, that the data in the database will be overwritten ?

  • Author

What do you mean with "but now I'd like to do it so whatever you type into the form will edit fields already in the database" ?

 

Do you mean that if the data given in the form already exist, that the data in the database will be overwritten ?

 

 

yes exactly that, sorry im tired!

 

basically if Dave 65000 is already in the database and i put in the form Dave 67500 I want the original field to update the latter.

 

Or even better have a drop down menu to select a name then input the points to change the field!

Edited by Willows

yes exactly that, sorry im tired!

 

basically if Dave 65000 is already in the database and i put in the form Dave 67500 I want the original field to update the latter.

 

Or even better have a drop down menu to select a name then input the points to change the field!

 

Do you have two seperate cells one for the name, and one for the points ?

  • Author

Do you have two seperate cells one for the name, and one for the points ?

 

 

Yes, so far everything works smoothly, the form works and I can ADD to the database, I just wish to OVERWRITE previous records as you said once I post the form.

 

Cheers

First we need to get the points from the user we enter in the form:

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$array=mysql_fetch_array($query);

 

So now we got the amount of point from the user submitted. (It stored in $array[0])

Now we procceed by checking if the amount of points is greater than the previous data.

( You can remove the if statement if you want to update it anyway )

So than we got:

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$array=mysql_fetch_array($query);
if ($array[0]<$value2)
{
$query2="UPDATE tourneytable SET Points='$value2' WHERE Name='$value'";
mysql_query($query2);
}

 

I suggest you check this very good for bugs because I'm a bit tired ;)

 

Hope it helped !

  • Author

First we need to get the points from the user we enter in the form:

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$array=mysql_fetch_array($query);

 

So now we got the amount of point from the user submitted. (It stored in $array[0])

Now we procceed by checking if the amount of points is greater than the previous data.

( You can remove the if statement if you want to update it anyway )

So than we got:

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$array=mysql_fetch_array($query);
if ($array[0]<$value2)
{
$query2="UPDATE tourneytable SET Points='$value2' WHERE Name='$value'";
mysql_query($query2);
}

 

I suggest you check this very good for bugs because I'm a bit tired ;)

 

Hope it helped !

 

 

SUPERB, works, only prob is I get an error code after submitting

 

"Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\tourneyleaguepost.php on line 24"

 

but it does actually update the field, thank you so much!

Edited by Willows

SUPERB, works, only prob is I get an error code after submitting

 

"Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\tourneyleaguepost.php on line 24

CONGRATS"

 

but it does actually update the field, thank you so much!

 

I think I know what I've done wrong;

mysql_fetch_array need a pointer to mysql_query.

Try this:

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$executequery=mysql_query($query);
$array=mysql_fetch_array($executequery);

 

Instead of

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$array=mysql_fetch_array($query);

 

Do you still get error codes?

  • Author

I think I know what I've done wrong;

mysql_fetch_array need a pointer to mysql_query.

Try this:

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$executequery=mysql_query($query);
$array=mysql_fetch_array($executequery);

 

Instead of

 

$query="SELECT points FROM tourneytable WHERE Name='$value'";
$array=mysql_fetch_array($query);

 

Do you still get error codes?

 

Works like a bloody charm my friend! you are a genius thank you so much, i've wracked my brains on this for hours and you have taken a massive weight off of my shoulders! If you available to help me a little further how would I go about coding the form to have a drop down menu full of the names rather name entering them manually? Obviously Its no biggie if you are too tired to help as I am pretty tired myself and could do with a break although if you could point me in the right direction that would be great, thanks again for your help!

Works like a bloody charm my friend! you are a genius thank you so much, i've wracked my brains on this for hours and you have taken a massive weight off of my shoulders! If you available to help me a little further how would I go about coding the form to have a drop down menu full of the names rather name entering them manually? Obviously Its no biggie if you are too tired to help as I am pretty tired myself and could do with a break although if you could point me in the right direction that would be great, thanks again for your help!

 

Well a drop down menu is just like a text input.

You place the <select> tag into your HTML document instead of the text input.

Than you can just read the value of it with the $_POST[''];

If you want to know more about the select tag visit:

http://www.w3schools.com/tags/tag_select.asp

 

So the only thing you have to do, is changing the text input with a select box.

Of course don't forget to set the ID and Name in the <select> tag equal to what you first have set it in the text input.

  • Author

Well a drop down menu is just like a text input.

You place the <select> tag into your HTML document instead of the text input.

Than you can just read the value of it with the $_POST[''];

If you want to know more about the select tag visit:

http://www.w3schools.com/tags/tag_select.asp

 

So the only thing you have to do, is changing the text input with a select box.

Of course don't forget to set the ID and Name in the <select> tag equal to what you first have set it in the text input.

 

 

okay great, suddenly its just stopped working, i havent even touched the code and its just not updating n e more, how perculiar!

 

 

EDIT:

 

Im an idiot, obviously i tryed to update a field with a smaller points figure to the one already stated, silly mistake. Thanks once again for all your help, im going to check the dropdown menu tut now and sort that out, thanks man!

Edited by Willows

okay great, suddenly its just stopped working, i havent even touched the code and its just not updating n e more, how perculiar!

 

 

Sure there is'nt something wrong with your server ?

What is your code ?

Well I think I'm going to sleep now ;)

This weekend I will not be at home ,

sunday I'm back to help you if you still got problems.

But I'm sure in the mean time there is somebody else on this forum that can help you ;)

 

Good night !

  • Author

Well I think I'm going to sleep now ;)

This weekend I will not be at home ,

sunday I'm back to help you if you still got problems.

But I'm sure in the mean time there is somebody else on this forum that can help you ;)

 

Good night !

 

 

Everything is perfect my friend, I cant thank you enough! Hit me up when your back and maybe I can give you some kind of payment for your troubles. THANK YOU!

Everything is perfect my friend, I cant thank you enough! Hit me up when your back and maybe I can give you some kind of payment for your troubles. THANK YOU!

 

I'm leaving at 9:15, just had enough time to leave a post ;)

I don't need a payment, little bit of good karma never hurted anybody,

You're welcome!

  • Author

I'm leaving at 9:15, just had enough time to leave a post ;)

I don't need a payment, little bit of good karma never hurted anybody,

You're welcome!

 

 

You're a true gent!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.