Jqgrid can Edit but not Add - php

I have my grid running and I can EDIT data but I cannot ADD new rows of data. When I submit the add form nothing happens and I get nothing back on my PHPMyAdmin page to show that the grid is hit the database with the INSERT statement.
Here is my Edit code.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "*****";
$dbname = "******";
// connect to the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());
mysqli_select_db($conn,$dbname) or die("Error conecting to db.");
//$id = "'".$_POST['id']."'";
$unit = "'".$_POST['unit_id']."'";
$div = "'".$_POST['div_id']."'";
$titleorg = "'".$_POST['title_org']."'";
$shortdesc = "'".$_POST['short_desc']."'";
$longdesc = "'".$_POST['long_desc']."'";
$unitdesc = "'".$_POST['unit_desc']."'";
$enabled = "'".$_POST['avail_ind']."'";
$active = "'Y'";
switch($_POST['oper'])
{
case "add":
$query = "INSERT INTO depunits (unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind,active_ind) values ($div,$unit,$titleorg,$shortdesc,$longdesc,$unitdesc,$enabled,$active)";
mysqli_query($conn,$query);
break;
case "edit":
$query = "UPDATE depunits SET unit_id=$unit,div_id=$div,title_org=$titleorg,short_desc=$shortdesc,long_desc=$longdesc,unit_desc=$unitdesc,avail_ind=$enabled where unit_id=$unit";
mysqli_query($conn,$query);
break;
}
?>

Related

How to insert IP address in table?

I'm coding an admin page where I keep track of users/visitors. I have some code so far, but I need to add ip addresses from the users/visitors to the table as well. This is my code, everything gets added to the database table except for ip address. The table is users4project and column is ip address with the int(10) UNSIGNED NOT NULL I created the table in phpmyadmin.
<?php
function visitor($record) {
// my database info
$db_host = "";
$db_username = "";
$db_password = "";
$db_name = "";
$db_table = "ipusers4project";
$counter_page = "access_page";
$counter_field = "access_counter";
$db = mysqli_connect ($db_host, $db_username, $db_password, $db_name)
or die("Host or database not accessible");
$sql_call = "INSERT INTO ".$db_table." (".$counter_page.",
".$counter_field.") VALUES ('".$record."', 1) ON DUPLICATE KEY UPDATE ".$counter_field." = ".$counter_field." + 1";
mysqli_query($db, $sql_call) or die("Error while entering");
$sql_call = "SELECT ".$counter_field. " FROM ".$db_table." WHERE ".$counter_page. " = '".$record. "'";
$sql_result = mysqli_query($db, $sql_call) or die("SQL request failed ");
$row = mysqli_fetch_assoc($sql_result);
$x = $row[$counter_field];
mysqli_close($db);
return $x;
}
?>
<?php
$ipadress = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO
ipusers4project
( ipadress )
VALUES
( '$ipadress')";
?>
EDIT: On index.php I have this code:
<?php
$page_name = "index.php";
?>
<title><?php echo $page_name; ?></title>
<?php
include "webcounter.php";
$access_number = visitor($page_name);
?>
Just add this as another column in the row that visitor() is adding.
<?php
function visitor($record) {
// my database info
$db_host = "";
$db_username = "";
$db_password = "";
$db_name = "";
$db_table = "ipusers4project";
$counter_page = "access_page";
$counter_field = "access_counter";
$ipadress = $_SERVER['REMOTE_ADDR'];
$db = mysqli_connect ($db_host, $db_username, $db_password, $db_name)
or die("Host or database not accessible");
$sql_call = "INSERT INTO ".$db_table." (".$counter_page.",
".$counter_field.", ipadress) VALUES ('".$record."', 1, '$ipadress') ON DUPLICATE KEY UPDATE ".$counter_field." = ".$counter_field." + 1, ipadress = VALUES(ipadress)";
mysqli_query($db, $sql_call) or die("Error while entering");
$sql_call = "SELECT ".$counter_field. " FROM ".$db_table." WHERE ".$counter_page. " = '".$record. "'";
$sql_result = mysqli_query($db, $sql_call) or die("SQL request failed ");
$row = mysqli_fetch_assoc($sql_result);
$x = $row[$counter_field];
mysqli_close($db);
return $x;
}
?>

Import data from mdb file to mysql database can not upload data

I'm using PHP code to upload or insert data from MDB file to MySQL database.I want my table value get inserted into MySQL database. But data does not inserted into MySQL database.This code shows me no error.Here is my code. please help I have tried every solution on net.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'payroll_system';
//mysql
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$sql = "SELECT * FROM attendance";
$result = mysql_query($sql);
//mdb
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\xampp\\htdocs\\payroll\\eTimeTrackLite1.mdb");
$rs = $conn2->Execute("SELECT * FROM AttendanceLogs");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$column1=$row["AttendanceLogId"];$column2=$row["AttendanceDate"];$column3=$row["EmployeeId"];$column4=$row["InTime"];
$column6=$row["OutTime"];$column8=$row["Duration"];
echo "hello";
echo $column1;echo $column2;echo $column3;echo $column4;
$rs->MoveFirst();
while (!$rs->EOF)
{
$attendance_id = $rs->Fields("AttendanceLogId");
$attendance_date = $rs->Fields("AttendanceDate");
$emp_id = $rs->Fields("EmployeeId");
$in_time = $rs->Fields("InTime");
$out_time = $rs->Fields("OutTime");
$duration = $rs->Fields("Duration");
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '"); ?>
<?php
$rs->MoveNext();
}
}
?>
</table> <?php
mysql_free_result($result);
$rs->Close();
$conn2->Close();
$rs = null;
$conn2 = null;
?>
you need to INSERT each records data instead of UPDATE. Re-write the following line:
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '");

How to update many fields with single query?

I am working with a MLM company where com has to register the member, if it detects any mistake in data, it has to update the data of member again.
I have the right code which works well when all fields has to update but problem is it does not work for individual input boxes. Please give me solution of this problem.
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'vvvv';
$dbpass = 'xxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$usrid = $_POST['uid'];
$pwrd = $_POST['pwd'];
$nm = $_POST['noe'];
$fnm = $_POST['fn'];
$addrs = $_POST['adrs'];
$cntn = $_POST['cnt_no'];
$cty = $_POST['ct'];
$sql = "UPDATE office_user ".
"SET
password = '$pwrd',
name='$nm',
father_name='$fnm',
address='$addrs',
contact_no='$cntn',
city='$cty' ".
"WHERE user_id = '$usrid'" ;
mysql_select_db('my_db');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

mysql_query error even though syntax of query is correct

$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
if ($_POST["invia_ricerca"]){
$nome = $_POST["nome"];
$query = " SELECT gruppo1 FROM elenco WHERE nome = '$nome' ";
$risultato = mysql_query($query, $conn) or die("suka");
echo $risultato;
}
If I write the query into phpmyadmin page it works. If I try to launch the query using mysql_query it doesn't work and print "suka" (die()). And if I try mysql_error() into die(), Nothing prints.
you are using mysqli_* functions for connection but in query you are using mysql_query.
it should by mysqli_query
change this
$risultato = mysql_query($query, $conn) or die("suka");
to
$risultato = mysqli_query($query, $conn) or die("suka");
=========== Edit====
$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
if ($_POST["invia_ricerca"]){
$nome = $_POST["nome"];
$query = " SELECT gruppo1 FROM elenco WHERE nome = '$nome' ";
$risultato = mysqli_query($query, $conn) or die("suka");
$row = $risultato->fetch_array(MYSQLI_NUM);
printf ("%s\n", $row[0]); //use %i for integer
}
$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
// Determine if a variable is set and is not NULL
if ( isset($_POST["invia_ricerca"]) )
{
// Escapes special characters in a string for use in an SQL
$nome = mysqli_real_escape_string($conn, $_POST["nome"]);
// Prepare Query
$query = sprintf("SELECT gruppo1 FROM elenco WHERE nome = '%s'", $nome);
// Performs a query on the database
$risultato = mysqli_query($query, $conn) or die( mysqli_error($conn) );
// Fetch a result row
while( $row = mysqli_fetch_array($risultato) )
{
printf("<p>%s</p>", $row['gruppo1']);
}
// Free result
mysqli_free_result($risultato)
}
PS. die("suka") รจ fantastico ;)
You have missed up mysqli with mysql. Try this:
$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
if ($_POST["invia_ricerca"]){
$nome = $_POST["nome"];
$query = " SELECT gruppo1 FROM elenco WHERE nome = '$nome' ";
$risultato = mysqli_query($conn,$query) or die("suka");
if ($risultato) {
while ($row = mysqli_fetch_assoc($risultato)) {
print_r($row);
}
mysqli_free_result($risultato);
}
}

Backing up a MySQL database using PHP

I was trying to make a backup of my MySQL db called "backup" using a PHP script below, but for some reason, it doesnt work. Any ideas what is wrong? I wanted to create a file called test.sql in the same folder that would contain the data (because the db is quite big I only selected values of Temp>35, but I could change that later). Right now when I run it, I get the echo, but no file created.
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$tableName = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * WHERE Temp>35 INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
echo "Backed up";
?>
Just Try.
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName WHERE Temp>35";
$result = mysql_query($query) or die(mysql_error());
Try this and let me know:
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$databasename = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $databasename WHERE Temp>35";
$result = mysql_query($query);
echo "Backed up";
?>
<?php
$source_db='source_db';
$target_db='target_db';
$server='127.0.0.1';
$user='root';
$password='';
mysql_connect($server,$user,$password);
mysql_select_db($source_db);
// Get names of all tables in source database
$result=mysql_query("show tables");
while($row=mysql_fetch_array($result)){
$name=$row[0];
$this_result=mysql_query("show create table $name");
$this_row=mysql_fetch_array($this_result);
$tables[]=array('name'=>$name,'query'=>$this_row[1]);
}
// Connect target database to create and populate tables
mysql_select_db($target_db);
$total=count($tables);
for($i=0;$i < $total;$i++){
$name=$tables[$i]['name'];
$q=$tables[$i]['query'];
mysql_query($q);
mysql_query("insert into $name select * from $source_db.$name");
}
?>

Categories