Jump to content

guys need help with Wampserver


phpboy

Recommended Posts

In that case, it sounds like you are simply using the wrong mysql username/password. The default username is "root" and there is no default password (""). If you have changed the username/password within MySQL, you would use that instead.

 

As a side note, can you stop randomly capitalizing parts of words? It makes it much harder to read what you are writing and it's annoying. :/

Link to comment
Share on other sites

  • Replies 106
  • Created
  • Last Reply

Top Posters In This Topic

Sir, i have here wewe.php

<?php

$con = mysql_connect("localhost","Root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}if (mysql_query("CREATE DATABASE my_db",$con))

{

echo "Database created";

}

else

{

echo "Error creating database: " . mysql_error();

}mysql_close($con);

?>

 

then the result is :

Error creating database: Access denied for user ''@'localhost' to database 'my_db'

 

why is that so?

Link to comment
Share on other sites

Sir, in my theSis,

i have done forms, Forms are for "INSERT" ," DELETE" and "UPDATE"

Yes! i have done all completely by myself with the use of php and mysql...

The scripts are all working..

In "INSERT" form

if i insert one item the inserted item will be inserted in my database and then i show the list of items in the same page so that it will act good ^_^

The same with the other 2 forms

 

But in "DELETE" form

If i deleted one item that is not in my database of course the scripts will just execute as what i designed to it..

But the real thing is that

i want to display also that if one item is deleted or updated and that item is not found inside my database, a promt or echo will say that "the item is not in the list"

i mean how can i do that? is there any ideas there Sir?:)

ty in advance

Link to comment
Share on other sites

Well, I suppose you could do two queries -- one to check if the row exists, then a second to actually do the update/delete query if it does exist. Here's a quick example:

 

// connect to database
mysql_connect('localhost','root','root');
mysql_select_db('test');

// select row
$result = mysql_query("SELECT * FROM test WHERE name='test'");
$row = mysql_fetch_array($result); 

// check if row exists
if (!$row == '') {
   // row exists, perform any additional database queries, such as deleting up updating
   echo 'success';
}
else
{
   // doesn't exist, show error message
   echo 'error';
}
?>

 

That said... The does slow down the site a bit, since you have to execute an extra query. Probably doesn't make a big difference at the moment, but for future reference, the more queries you do, the more difficult it is on your database and the slower your site will be. This is primarily for sites with large amounts of traffic, though.

 

Also, if you actually need this above code, I think whatever you are writing is flawed. The way I see it, you want PHP to act as a buffer layer between your user and the database. The user can't have direct access to the database, since that will greatly increase the chances that the user will edit the database in such a way that it makes your app stop working. Realistically, the site should only allow the user to delete or update items that are already in the database. If it is showing items that no longer exist, or you are allowing the user to enter part of the query in a input box and then submit it, you have some security problems.

 

Make sense?

Link to comment
Share on other sites

Perhaps there is a different way to do what you are wanting to achieve... Giving the user direct control like that seems like it is asking for trouble. It would be much safer to only allow the user to delete items that are already in the database.

 

Think about this...

 

You didn't mention, but I'll assume you have a basic page that displays all of the items in your library, probably using something like this to display the items in a table:

 

>$result = mysql_query("SELECT * FROM tablelibrary");

echo "</pre>
<table border="1">

Name
Author
Number of Books
";

while($row = mysql_fetch_array($result))
 {
 echo "";
 echo "" . $row['Name'] . "";
 echo "" . $row['Author'] . "";
 echo "" . $row['Books'] . "";
 echo "";
 }
echo "</table>"

 

Within that loop, you could add a new column in your table that would include delete links for each item. This would be much more convenient for the user, because it reduces the chances of errors and doesn't force them to enter in the books they want deleted manually. (If you want some sort of user administration system, you could check if they were logged in first, and only display the links if they had the right authorization).

 

To do that, you would need to add an extra column to your existing database table, "bookId" (assuming you don't already have this), since you need some way of uniquely identifying each book. The "bookId" column would be the primary key (which means it is unique per row in the table) and would have the "auto_increment" property, which means that you don't have to set it -- PHP will automatically add a unique value to that column every time you add a new row. You will probably need this anyway; if you don't have it, since what happens if two books are named the same? You'll run into problems if you try to delete a book based only on their name.

 

More info:

http://www.tech-recipes.com/rx/377/create-a-mysql-table-with-a-primary-key/

http://www.hscripts.com/tutorials/mysql/insert-auto-increment.php

 

This could be the line in your code that adds the delete link (add it within that loop I showed you above):

 

echo 'delete';

 

This would add a new column that contains a "delete" link that would link to "delete.php?id="

 

Then, in delete.php, you would grab the "id" field using $_GET, perform the MySQL delete query, deleting the row that had the id that you grabbed using $_GET, and then immediately redirect back to the previous page once the query had completed using the header function.

 

http://www.w3schools.com/php/func_http_header.asp

 

This doesn't necessarily need to be limited to just the delete function. What happens if the user need to edit an existing book? You could do a similar thing as I showed above, except adding an edit link and linking to edit.php.

Link to comment
Share on other sites

  • 2 weeks later...

It's been a while since I looked at this thread...

 

Basically, I'm just suggesting a different way of deleting entries. Basically, you would have a browse page that showed all entries in your database, and give a link to a delete page. When users click the link (for example, "delete.php?id=3") PHP would parse the URL for the id# of the item you wanted deleted (item #3, in this case), perform the delete, and then redirect back to the browse page. Your basic setup would look like this:

 

-- Browse page (shows all entries, and provides links to edit and delete existing entries)

-- Delete page (only accessible via the browse page, would delete the correct entry based on the id in the URL and redirect back to the browse page)

-- Insert page (Would allow users to insert new entries)

 

I'm not totally sure how you have things set up on your end. However, I am currently writing a administration panel for my blog on my site that can add/edit/delete blog posts, etc. and this is the method that I am using.

 

With my code, you would leave the insert.php alone -- that doesn't need to be modified. You'll need to modify your delete.php to get the id from the URL and delete the correct entry based on that.

 

As I said, I am working this at the moment myself... If you want, I can put together a quick screencast showing how my system works if you need more explanation, but I probably won't have time to do that until the end of this week/the weekend.

Link to comment
Share on other sites

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}mysql_select_db("library", $con);$sql="DELETE FROM tablelibrary WHERE (id)='$_POST[id]'";if (!mysql_query($sql,$con))

{

die('Error: ' . mysql_error());

}

 

 

echo "record/s deleted";

$result = mysql_query("SELECT * FROM tablelibrary");

 

echo "

Name

Author

Number of Books

";while($row = mysql_fetch_array($result))

{

echo "

echo "

" . $row['Name'] . "";

echo "

" . $row['Author'] . "";

echo "

" . $row['Books'] . "";

echo "

";

 

}

 

echo "

";mysql_close($con);

?>

Link to comment
Share on other sites

Looks like you are using $_POST on deleteben2.php, when you should be using $_GET. I would need to test the code, but I believe that should clear things up.

 

so this: $sql="DELETE FROM tablelibrary WHERE (id)='$_POST[id]'";

should be this: $sql="DELETE FROM tablelibrary WHERE (id)='$_GET[id]'";

 

If not, can you do a export of your database so I can take a look at the code?

 

To do that:

-- log in to PHPMyAdmin

-- select the correct database

-- click the "export" tab

-- click "go"

-- either post or PM me the resulting SQL code

 

...oh, and you really don't need to call me "sir". I'm only 22 -- that just makes me feel old. :rolleyes:

Link to comment
Share on other sites

bro! ^_^

i have sent u the codes u want.....

ei i'm asking at the my deleteben.php

there's a code there with the links to deleteben2.php

how can i make the deleteben1.php that

in every click of the link "delete"

it will automatically delete the item and back again to the page

Edited by phpboy
Link to comment
Share on other sites

You could just do something like this... (assuming you fixed the GET/POST problem I pointed about above.)

 

Your deleteben2.php code could be much simpler... you really don't necessarily need to display any html. Just do something like this:

 

<?php

 

// connect to the database

// get the id from the URL using $_GET (probably want to check if it a valid value, if not, redirect back to deleteben.php)

// run the delete MySQL query

// redirect back to deleteben.php

 

?>

 

To redirect, you need to use the header() function, like this:

 

header("location: deleteben.php");

Link to comment
Share on other sites

bro...i do have nothing to change in my deleteben.php?

i try to do it but i failed

here's my code for deleteben2.php

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}mysql_select_db("library", $con);

$sql="DELETE FROM tablelibrary WHERE (id)='$_get[id]'";

header("location: deleteben1.php");

?>

 

and probably it doesnt run correctly..

can u give me your codes for it?? so that i can go on to my "UPDATES" page >_<

 

another question..

how can i use the $_GET in a link??

i mean in using form we usually put 'names' in input types then put in our php page , $_GET['names']

Link to comment
Share on other sites

deleteben.php should be fine

 

this line

 

$sql="DELETE FROM tablelibrary WHERE (id)='$_get[id]'";

 

needs to be

 

$sql="DELETE FROM tablelibrary WHERE (id)='$_GET[id]'";

 

(get needs to be uppercase) also, make sure your header statement redirects back to the right file... I'm not sure if that is deleteben.php or deleteben1.php

Link to comment
Share on other sites

Rephrase this? I'm not sure I understand the question.

 

another question..

how can i use the $_GET in a link??

i mean in using form we usually put 'names' in input types then put in our php page , $_GET['names']

 

Just noticed.. you never actually perform the query in your deleteben2.php file. You'll need to add this:

 

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

Link to comment
Share on other sites

what i mean is:

in ur codes:

echo '

delete';

this is the link at this code

deleteben2.php?id='

in the 'id='

do i have to put name there after equals?

then in my page use the method $_GET['(name that i will put in id='']

 

waaa can u give me ur codes pls

codes for a form showing my supposed to be what i want... for delete page and delete.php for every 'delete' link

ty

Link to comment
Share on other sites

what i mean is:

in ur codes:

echo '

delete';

this is the link at this code

deleteben2.php?id='

in the 'id='

do i have to put name there after equals?

then in my page use the method $_GET['(name that i will put in id='']

 

waaa can u give me ur codes pls

codes for a form showing my supposed to be what i want... for delete page and delete.php for every 'delete' link

ty

 

If you want to include the name in the url, yes, you would do this:

 

echo '

delete';

 

probably would want to change the url to "name=" rather than "id="

 

I can take a look at your code and get it working, but it'll have to be later today when I have access to my PHP setup.

Link to comment
Share on other sites

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}mysql_select_db("library", $con);$sql="DELETE FROM tablelibrary WHERE (id)='$_POST[id]'";if (!mysql_query($sql,$con))

{

die('Error: ' . mysql_error());

}

 

 

 

echo "record/s deleted";

$result = mysql_query("SELECT * FROM tablelibrary");

 

echo "

Name

Author

Number of Books

";while($row = mysql_fetch_array($result))

{

echo "

echo "

" . $row['Name'] . "";

echo "

" . $row['Author'] . "";

echo "

" . $row['Books'] . "";

echo "

";

 

}

 

echo "

";mysql_close($con);

?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...