dynamic input box to update insert and delete in my sql - php

I have a simple form but I want to dynamically change the form action from insert to update in same page and I want to show the values in the same page. I have written update in href link. Please help me.
<?php
$username="root";
$password="";
$database="test";
include"db.inc.php";
$order = "SELECT * FROM tablename1";
$result = mysql_query($order);
while ($row=mysql_fetch_assoc($result)){
echo ("<tr><td>$row[sno]</td>");
echo ("<td>$row[name]</td>");
echo "<td >Edit</td>";
echo "<td>Delete</td></tr>";
}
?>

i thought you are asking dynamic insert/update/delete in single page.
if it is a case u should use ajax call.after get the response from server, change your form as insert/update/delete dynamically through javascript
if u want to learn ajax start with this
http://www.w3schools.com/ajax/default.ASP
i guess it might helpful to u.........

Related

How to make button to remove value in mysql field?

I am having difficulty making a button that removes existing values in MySQL database field. I have read up some information on it, some say use AJAX, but i'm not sure. What are your opinions? Can any Stackoverflow-er show me a method? I recognise I cannot use Javascript as it is on the client side and not server-side - but is there another way?
create form first in html
<form method="get" action="delete.php">
Enter id you want to delete : <input type="text" name="id">
<input type="submit">
</form>
delete.php
<?php
$dd=$_GET["id"];
$c=mysql_connect("localhost","root","");
mysql_select_db("mydatabase")
mysql_query("delete form mytable where id=$dd");
print "Remove successfully...";
?>
This upper code will remove the entries you want on clicking of submit button
If you want to remove the values from mysql database asynchronously then you'll need to use AJAX and that's the only option else if you are allowed to do it without refreshing the page then go with simple PHP.
you need not to do it with ajax if u dont want. simple php is good enuf..
just make a form with a button field.
post a variable though it to your php.
connect with your database using update the dtabase as you want..
Beware : One refresh would take place on this process as its not an asynchronous process. else you can choose AJAX
you could use a (post/get) request to a file on your server but i recommend you use AJAX to do this, the server side file will still look pretty much the same.
if you are using php for the back end the code would look like
$mysqli = new mysqli("localhost", "user", "password", "database");
$res = $mysqli->query('your delete statement');
if(! $mysqli->errorno )
header('Location:success url');
else
header('Location:fail url?error=' . $mysqli->error')
you could use a link for you button
<a class="button" href="script to delete">delete unwanted data</a>

update mysql database using the submit button on the colorbox

Index page list all the data from mysql & have a edit link for each data. When clicking the edit link it popup edit page within colorbox. the colorbox contain 2 text fields & an update button.
<script>
$(document).ready(function(){
$('.ajax').colorbox();
});
</script>
When clicking the submit button, updates the new data to mysql within the colorbox.
Please anyone help me on this..
thanks!
Its a simple to update this.
First: Get the data from html form.
Second: Send a request via Ajax.
Third: Update this using php.
PHP CODE
<?php
$prop_adr = $_POST['property_Address']; // or whatever your variable
$prop_zip = $_POST['property_Zip']; // or whatever your variable
// provide the correct info in mysql_connect();
$sql =mysql_connect('mysql_server(localhost)','mysql_user(root)','mysql_pass(empty)');
$db = mysql_select_db('your database name',$sql);//provide the database name
// UPDATE you table.
mysql_query("UPDATE `table_name` SET `address` = '$prop_adr'
WHERE `zipcode` = '$prop_zip'");
?>

Inserting values into form fields --PHP

I get values from an HTML form using a post method and store them in my DB through the mysql INSERT command (done in PHP).
something like this:
$value = $_POST['name'];
$value1 = $_POST['age'];
$sql = "INSERT INTO table(name,age) VALUES('$value','$value1')" ;
Now what I want to do is I want to give a link in the same PHP file like:
echo 'To update just entered info click on me';
Now the rascal.php as mentioned in the link above also contains the same form fields (name and age).
The values stored in the database through the INSERT command above should be fetched from the DB and placed in the respective form field area (name and age should be stored in their respective forms of rascal.php).
How can I do this?
Here it is:
<?php
if(isset($_GET['name']) AND isset($_GET['age'])){
$name=$_GET['name'];
$age=$_GET['age'];
}
else{
if(!empty($_POST['name']) AND !empty($_POST['age'])){
$value=mysql_real_escape_string($_POST['name']);
$value1=mysql_real_escape_string($_POST['age']);
$sql = "INSERT INTO table(name,age) VALUES('$value','$value1')" ;
mysql_query($sql);
echo 'To update just entered info click on me';
}
}
?>
<form method="POST">
<input name="name" value="<?php print $name;?>"/>
<input name="age" value="<?php print $age;?>"/>
</form>
This should work if you change 'table' to your table name. You should be happy, because I wrote the entire code for you. Please show it.
Further advice: Make sure your code is save by passing all input variables through a mysql_real_escape_string() function. Add an id field (unique key, self incrament) to your database table so you can select records based on their id's and not some other field (The code I provided only works if you don't have more entries with the same name).
There are some great php tutorials here.
Bobby Jack is exactly right about the danger of this, but I'll try to answer your question anyway.
Why not do it without touching the database? You could modify the link to rascal.php like so:
echo 'To update just entered info click on me';
And then use $_REQUEST['name'] to get that information in rascal.php. This way you are assured the same values are carried over.
Another way could be to use the mysql LAST_INSERT_ID and fetch the information that way, though it would not be 100% reliable, you could use that id for a query to populate the form as well.
EDIT: You could use LAST_INSERT_ID and put that id in the link for rascal.php, or use it in rascal.php itself to query the information you need.

passing href attributes to another php page

.a little help guys..
I have an index.php page which contains tags which when clicked opens up picture.php.
.my picture.php is a page that retrieves images from a certain database depending on what is the idno.
.what i want to do is to add an "idno" attribute on my anchor tag that when the user clicks a certain tag the index.php sends the idno to my picture.php so that the picture.php knows what image to fetch from the database. help pls!
So for your anchor tag, could you do something like this?
<?php
$url = "pictures.php?idno=" . idno;
echo "Click Me";
?>
Now in your pictures.php file, you can read the ID like this:
$idno = $_GET['idno'];
The only thing to watch out for is to make sure you sanitize that input, before you pass it off to a database query so you aren't vulnerable to a SQL injection attack.
You can send the parameter to picture.php via GET
Blah!
You could use GET to pass idno to pictures.php
so your link would look like this:
<a href='/picture.php?idno=12345678'>My Picture Tag</a>
In your picture.php:
$idno = $_GET['idno'];
Make sure you sterilize $idno before using it in a MySQL query. If idno is a number, then you can also check if it's an integer:
$isInt = is_int($idno);
Picture # <?php echo $idno ?>
Figuring out how to set $idno is left as an exercise to the OP.

Help with deleting items in a list, using PHP

I have a list of email addresses in a table, which is populated via an SQL query. Next to each email address, I've placed a submit button which I want to use to delete the email address that appears in that particular table row.
I thought of appending the email address to the name of each delete button, with the hope that it will take me in the right direction.
while($row = mysql_fetch_array($result))
echo "<tr><td>".$row['email_adress']."</td><td><input type=\"submit\" value=\"Delete\" name=\"delete".$row['email_adress']."\"></td></tr>";
I'm wondering how to use the delete button for each entry. Any help?
I think the issue is that you are attempting to use submit buttons to contain data when submit buttons are not meant to contain data. Well they can and people do use them that way, but I prefer to use submit buttons to determine how to handle the data rather than to be the data in the submit. There are two basic methods that I think would be a fair extension to what you are trying to do.
1.) Use links to issue the delete
2.) Use the submit button to issue the delete, but include a hidden form field to contain the email address to delete.
In the case of using a link you don't have to deal with a form, but I don't know about the rest of the page. If you are sending any other data, then a form is the ideal way. If you are just sending a single piece of data to delete the email, then I suggest a link. My recommendation come from a functional perspective. You might have a UI reason to a button that I don't know about.
In the case of using a form, the hidden field should contain the primary key (untested):
while($row = mysql_fetch_array($result)) {
echo "<tr>
<td>".$row['email_adress']."</td>
<td><form action="some/uri/to/something">
<input type=\"hidden\"
name=\"emailAddress\" value=\"{$row['email_adress']}\"/>
<input type=\"submit\" value=\"Delete\"
name=\"delete".{$row['email_adress']}."\">";
</form>
</tr>
</td>";
}
You have to create a form around each submit else every hidden field in the form would be submitted telling you nothing about which email to delete. You can do many more things with the submit by using Javascript, but the simple method would be to just use a link -- forget about forms.
If a button is absolutely a must, you could output a small form for each button and add a hidden field containing the id (or email, if you insist). The form would submit the id or address in the hidden field, which you pick up in your processing script and run your delete query.
edited to remove a portion of the answer that was determined to be an unwise method
so I've been messing around with this code trying to get a "prefetch browser add-on" to trigger something and kill my database... but I havn't been able to trigger anything yet... but I'd sure like to.
<?php
//CREATE AND POPULATE DB
if(!file_exists('a.sqlite')){
$db = new SQLite3('a.sqlite');
$db -> exec("CREATE TABLE test (idx INTEGER PRIMARY KEY, number INTEGER NOT NULL);");
for($i=0;$i<9;$i++){
$db -> exec("INSERT INTO test (number) VALUES (".(rand(1000,9999)).");");
}
}else{
$db = new SQLite3('a.sqlite');
}
//PERFORM DELETE FROM HREF
if(isset($_REQUEST['delete'])){
$db -> exec("DELETE FROM test WHERE idx = ".$_REQUEST['delete'].";");
}
//SHOW CONTENTS OF BASE OR DELETE DB FILE
$result = $db->query('SELECT * FROM test');
while ($row = $result->fetchArray()) {
echo "<a href='test.php?delete=".$row['idx']."'>".$row['number']."</a></br>";
}
?>
Yes, you could do it like this but that would require to either execute the query again on the form's target page and then check for every combination of "delete".$row['email_adress'] or use a construction like this:
foreach ($_REQUEST as $key => $dummy)
if (substr($key,0,6) == 'delete')
delete_entry(substr($key,6));
However, if you simply swap value and name...
<input type=\"submit\" name=\"Delete\" value=\"".$row['email_adress']."\">
...you can just check $_REQUEST['Delete'] to find out which address button has been clicked.
... I'm still not sure about displaying peoples emails on web pages unless it's company internal.. I gather we are in a secure company back office?
I'd go for the:
<a href=my_page.php?delete_email_id=456>email</a>
probably with some annoying javascript for the confirm
email
and catch it with a
if(isset($_REQUEST['delete_email_id']){ SQL UPDATE ENTRY...}
at the top of my_page.php

Categories