8 Replies - 821 Views - Last Post: 23 August 2009 - 02:40 PM Rate Topic: -----

#1 grim1617  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-August 09

Cannot Query MySQL

Post icon  Posted 22 August 2009 - 09:09 PM

I need to create a credit system but the DB query is not working and I'm not sure if I am using the correct code.

I put up a login system with a row called "credits" where I can give user credits and take them away. I need the code to be able to update the users credits using a form.

pointadd.php is where they use the form to add credits

<html>
<body>

<h2>Add Credits</h2>


<form action="addcredit.php" method="post">
Give credits to:
<input type="text" name="givegcreditnname"><br />
Amount of credits(negative to remove):
<input type="text" name="addcredits"><br />
<input type="submit"><br />
</form>

</body>
</html>


addcredit.php is used to UPDATE the amount of credits they have.

<?php

$give_credit_name = $_POST['givecreditnname'];

$add_credits = $_POST['addcredits'];


$dbc = mysqli_connect('...', '...', '...', '...')
or die('Could not connect to Database');

$query = "UPDATE users WHERE username = '$give_credit_name' SET credits = '$add_credits'" .
"VALUES('$add_credits')";

mysqli_query($dbc, $query)
or die('Could not query');

mysqli_close($dbc);

echo 'Points add or Subtracted.';

?>


Each time I got on the page i get the "Could not Query".
Anyone know whats wrong, I am using php 5 and mysql 5.1 I believe.

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Query MySQL

#2 Redian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 28-December 07

Re: Cannot Query MySQL

Posted 22 August 2009 - 09:39 PM

Have you tried using or die(mysqli_error)? It should return exactly the error. I'm not too familiar with the OOP mysqli object myself.

EDIT:
I think the problem may be concatenation and variable incorporation. Try this:
$query = "UPDATE users WHERE username = '".$give_credit_name."' SET credits = '".$add_credits."' VALUES('".$add_credits."')";


This post has been edited by Redian: 22 August 2009 - 09:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Cannot Query MySQL

Posted 22 August 2009 - 09:45 PM

your query should be like this $query = "UPDATE users SET credits = '$add_credits' WHERE username ='$give_credit_name'";
Was This Post Helpful? 0
  • +
  • -

#4 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Cannot Query MySQL

Posted 22 August 2009 - 10:54 PM

like noorahmad said, your query is backwards. SET always needs to be before WHERE in a MySQL or MySQLi query.
Was This Post Helpful? 1
  • +
  • -

#5 grim1617  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-August 09

Re: Cannot Query MySQL

Posted 23 August 2009 - 07:25 AM

Well, it does query now, but it is not updating, it is just inserting credits into a new row OR it just does not do it when I check the database.
Do I need to do something like SELECT FROM?
Here is what I used in the DB:

CREATE TABLE users (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(60), password VARCHAR(60), credits int NOT NULL)

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Cannot Query MySQL

Posted 23 August 2009 - 07:59 AM

Try:
1. Echo the query just before running it. Check the echo to make sure it's the query you expect.

2. Use phpmyadmin (or whatever utility you have) to check the database and be sure that the where part of your query is pointing to something that exists. With phpmyadmin, you could copy/paste the query and run it from there to test variations on it.
Was This Post Helpful? 0
  • +
  • -

#7 grim1617  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-August 09

Re: Cannot Query MySQL

Posted 23 August 2009 - 12:45 PM

Well it worked in phpmyadmin...weird
So something wrong with my php?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Cannot Query MySQL

Posted 23 August 2009 - 12:55 PM

More likely there's a slight but important difference between what you have in the php query and what you're using in phpmyadmin.
Was This Post Helpful? 0
  • +
  • -

#9 grim1617  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-August 09

Re: Cannot Query MySQL

Posted 23 August 2009 - 02:40 PM

EDIT: I found the Error! And thanks everyone, I wouldn't have been able to do this without you!
I'll come back and bug you guys if I need help again ;)

This post has been edited by grim1617: 23 August 2009 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1