Jump to content

my update and add isn't working/..


syntax556

Recommended Posts

Hey guys been asking a bunch of my friends that know some php to try and help me with this issue im having with no success .. so i come here hoping to get answers i need.

 

The delete function works but add and update dont work.

<?php session_start(); require 'approve.php'; ?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Contacts</title>
  
  <!-- Include jQuery -->
  <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
  
  <style type='text/css'>

    body {
   background-color: #333;  
   color: #999;
   font: 12px/1.4em Arial,sans-serif;
}
#wrap {
   margin: 10px auto;        
   background: #666;
   padding: 10px;
   width: 1024px;
}
#header {
   background-color: #666;
   color: #FFF;
}
#logo {
   font-size: 30px;  
   line-height: 40px;    
   padding: 5px;
}
#navWrap {
   height: 30px;
}
#nav {
   padding: 5px;
   background: #999;    
}
#nav ul {
   margin: 0;
   padding: 0;    
}
#nav li {
   float: left;
   padding: 3px 8px;
   background-color: #FFF;
   margin: 0 10px 0 0;
   color: #F00;
   list-style-type: none;
}
#nav li a {
   color: #F00;  
   text-decoration: none;    
}
#nav li a:hover {
   text-decoration: underline;   
}
br.clearLeft {
   clear: left;        
}​

  </style>
  


<script type='text/javascript'>
//<![CDATA[ 

  $(function() {
    // Stick the #nav to the top of the window
    var nav = $('#nav');
    var navHomeY = nav.offset().top;
    var isFixed = false;
    var $w = $(window);
    $w.scroll(function() {
        var scrollTop = $w.scrollTop();
        var shouldBeFixed = scrollTop > navHomeY;
        if (shouldBeFixed && !isFixed) {
            nav.css({
                position: 'fixed',
                top: 0,
                left: nav.offset().left,
                width: nav.width()
            });
            isFixed = true;
        }
        else if (!shouldBeFixed && isFixed)
        {
            nav.css({
                position: 'static'
            });
            isFixed = false;
        }
    });
});

//]]>  
</script>


</head>
<body>

<div id="wrap">
    
    <!-- The header code, including the menu -->
    <div id="header">
        <div id="logo">Contacts<br /></div>
        <div id="navWrap">
            <div id="nav">
                <ul>
                    <li><a href="#" class="smoothScroll"><a href="export.php">Export Contacts</a></a></li>
                </ul>    
                <br class="clearLeft" />
            </div>
        </div>
    </div>
    
<?php

$separate = '';

ini_set('display_errors', 'On');
ini_set("xdebug.var_display_max_depth", -1);
ini_set("xdebug.var_display_max_children", -1);
ini_set("xdebug.var_display_max_data", -1);
$con = mysql_connect("66.150.xxx.xxx","contact","xxx");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("zadmin_contact",$con);
 
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE contact SET ID='$_POST[id]',Title='$_POST[title], First Name='$_POST[firstname]', Last Name='$_POST[lastname]', Company='$_POST[company]', Job Title='$_POST[jobtitle]', Business Phone='$_POST[businessphone]',Home Fax='$_POST[homefax]', Home Phone='$_POST[homephone]', Mobile Phone='$_POST[mobilephone]', E-mail Address='$_POST[email]' WHERE Name='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
 
if (isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM contact WHERE ID='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
 
 
if (isset($_POST['add'])){
$AddQuery = "INSERT INTO contact (ID, Title, First Name, Last Name, Company, Job Title, Business Phone, Home Fax, Home Phone, Mobile Phone, E-mail Address) VALUES ('$_POST[uid]','$_POST[utitle]', '$_POST[ufirstname]', '$_POST[ulastname]', '$_POST[ucompany]', '$_POST[ujobtitle]', '$_POST[ubusinessphone]', '$_POST[uhomefax]', '$_POST[uhomephone]', '$_POST[umobilephone]', '$_POST[uemail]')";
mysql_query($AddQuery, $con);
};
 
 
 
$sql = "SELECT * FROM contact";
$myData = mysql_query($sql,$con);
echo "<table border=1 bgcolor=#FFFFFF align=center>
<tr>
<th>ID</th>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Company</th>
<th>Job Title</th>
<th>Business Phone</th>
<th>Home Fax</th>
<th>Home Phone</th>
<th>Mobile Phone</th>
<th>E-mail Address</th>
</tr>";
while ($record = mysql_fetch_array($myData))
{
echo "<form action=\"dashboard.php\" method=\"post\">";
echo "<tr>";
echo "<td>" . "<input type=\"text\" name=\"id\" value=\"" . $record['ID'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"title\" value=\"" . $record['Title'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"firstname\"  value=\"" . $record['First Name'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"lastname\"  value=\"" . $record['Last Name'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"company\"  value=\"" . $record['Company'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"jobtitle\"  value=\"" . $record['Job Title'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"businessphone\"  value=\"" . $record['Business Phone'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"homefax\"  value=\"" . $record['Home Fax'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"homephone\"  value=\"" . $record['Home Phone'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"mobilephone\"  value=\"" . $record['Mobile Phone'] . "\"> </td>";
echo "<td>" . "<input type=\"text\" name=\"email\"  value=\"" . $record['E-mail Address'] . "\"> </td>";
echo "<input type=\"hidden\" name=\"hidden\" value=\"" . $record ['ID'] . "\">";
echo "<td>" . "<input type=\"submit\" name=\"update\" value=\"update\"> </td>";
echo "<td>" . "<input type=\"submit\" name=\"delete\" value=\"delete\"> </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=\"dashboard.php\" method=\"post\">";
echo "<tr>";
echo "<td><input type=\"text\" placeholder=\"ID\" name=\"uid\"></td>";
echo "<td><input type=\"text\" placeholder=\"Title\" name=\"utitle\"></td>";
echo "<td><input type=\"text\" placeholder=\"Fist Name\" name=\"ufirstname\"></td>";
echo "<td><input type=\"text\" placeholder=\"Last Name\" name=\"ulastname\"></td>";
echo "<td><input type=\"text\" placeholder=\"Company\" name=\"ucompany\"></td>";
echo "<td><input type=\"text\" placeholder=\"Job Title\" name=\"ujobtitle\"></td>";
echo "<td><input type=\"text\" placeholder=\"Business Phone\" name=\"ubusinessphone\"></td>";
echo "<td><input type=\"text\" placeholder=\"Home Fax\" name=\"uhomefax\"></td>";
echo "<td><input type=\"text\" placeholder=\"Home Phone\" name=\"uhomephone\"></td>";
echo "<td><input type=\"text\" placeholder=\"Mobile Phone\" name=\"umobilephone\"></td>";
echo "<td><input type=\"text\" placeholder=\"E-mail Address\" name=\"uemail\"></td>";
echo "<td>" . "<input type=\"submit\" name=\"add\" value=\"add\"></td>";
echo "</form>";
echo "</table>";
 
 
 
mysql_close($con);

?>
<p>Copyright © <?php echo date("Y"); ?> Kuro. All rights reserved.</p>
Edited by syntax556
Link to comment
Share on other sites

Table column names should not contain spaces. If you insist on them, surround them with backticks in the query (`First name`).

 

Also your update is missing the the closing single quote after $_POST[title].

 

You should not put data submitted by users directly into queries ($POST, $_GET, $_COOKIE) as it is a huge security risk. Escape the data with mysql_real_escape_string() or, better, move to mysqli or PDO where you can use prepared statements

Link to comment
Share on other sites

Table column names should not contain spaces. If you insist on them, surround them with backticks in the query (`First name`).

 

Also your update is missing the the closing single quote after $_POST[title].

 

You should not put data submitted by users directly into queries ($POST, $_GET, $_COOKIE) as it is a huge security risk. Escape the data with mysql_real_escape_string() or, better, move to mysqli or PDO where you can use prepared statements

i have added the following but still did not make any changes still unable to add or update any information.

Link to comment
Share on other sites

sorry for double post but what if i renamed all table column's without spaces.

<?php
include('db.php');

//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

//select table to export the data
$select_table=mysql_query('select * from contact');
$rows = mysql_fetch_assoc($select_table);

if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

// get total number of fields present in the database
function getcsv($no_of_field_names)
{
$separate = '';


// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
{
if (preg_match('/\\r|\\n|,|"/', $field_name))
{
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';
}

//make new row and line
echo "\r\n";
}
?>

and used this code to export into csv but i want to make it so when it exports it changes them from like First_Name to First Name. how would i do this.

Edited by syntax556
Link to comment
Share on other sites

that was not the question at all....

 

sorry for double post but what if i renamed all table column's without spaces.

<?php
include('db.php');

//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

//select table to export the data
$select_table=mysql_query('select * from contact');
$rows = mysql_fetch_assoc($select_table);

if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

// get total number of fields present in the database
function getcsv($no_of_field_names)
{
$separate = '';


// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
{
if (preg_match('/\\r|\\n|,|"/', $field_name))
{
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';
}

//make new row and line
echo "\r\n";
}
?>

and used this code to export into csv but i want to make it so when it exports it changes them from like First_Name to First Name. how would i do this.

 

If you have phpMyAdmin or other dbms frontend admin program you can just edit the column names.

that was not the question at all... try reading the question then answer. i asked how i could make it so it renames stuff from like First_Name to First Name when exporting to csv in the code adove.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.