I want to change my field comment using php but there is a problem!!
I need to get other column`s features :|
this is my code(gets all column features):
$query = "
SELECT
*
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = '$dbName' AND
TABLE_NAME = '$tableName' AND
COLUMN_NAME = '".$row->name."'";
$result = mysql_query($query) or die($query.'<br>'.mysql_error());
$tempRow = mysql_fetch_object($result);
this is my code that changes the comment
$query = "ALTER TABLE `$tableName`
MODIFY `".$row->name."`
".$tempRow->COLUMN_TYPE."
DEFAULT ".$tempRow->COLUMN_DEFAULT."
COMMENT '$comment'" ;
$result = mysql_query($query) or die($query.'<br>'.mysql_error());
but problem is that i lose some features , for example , outo increament , primary key ...
is there any way that i dont need to write all features , just change comment like update query ?!! if not how I should correct this query?!
Sorry about that, but there is no way to just change a single feature. You always have to specify all features that the column has. So extend your query to reflect all the column's features. All you need for that should be in $tempRow.
if ($tempRow->IS_NULLABLE == "NO")
{
$nullStr =" Not null ";
}else
{
$nullStr =" null ";
}
if (strlen($tempRow->COLUMN_DEFAULT) >0 )
{
$defaultStr = "DEFAULT ".$tempRow->COLUMN_DEFAULT;
}else
{
$defaultStr =" ";
}
if (strlen($tempRow->COLLATION_NAME )>0)
{
$collateStr = "collate " . $tempRow->COLLATION_NAME ;
}
$comment .= implode("|",$info);
$query = "ALTER TABLE `$tableName`
MODIFY `".$row->name."`
".$tempRow->COLUMN_TYPE."
".$nullStr."
".$extraStr."
".$defaultStr."
". $tempRow->EXTRA."
". $collateStr."
COMMENT '$comment'" ;
Related
I want to update data row
But the problem is that it is updated All rows
this is my code:
$query = mysql_query("SELECT *
FROM emp
JOIN inv ON emp.name=inv.empname
WHERE inv.empname='".$name."'")
or die ("mysql error query");
$id = $_POST['id'];
$updatestartdate = date('d/m/Y');
$updateenddate = date('d/m/Y');
$status = $_POST['status'];
while ($rowshow = mysql_fetch_assoc($query)){
$timetoshow = unix_time($rowshow['timex']);
//UPDATE
if (isset($_POST['Update']) and $_POST['Update'] == 'dataupdate'){
$updatestatus = mysql_query ("UPDATE inv SET
updatestartdate='$updatestartdate',
updateenddate='$updateenddate',
status='$status'
WHERE id='".$rowshow['id']."'")
or die ("updatestatus Error");
if (isset ($updatestatus)){
echo "<div class='hidecontent'><h3 style='background-color:#3F3F3F; padding:5px;' align='center'>
<font color='#FFFFFF'>Update is done</font></h3><meta http-equiv='Refresh' content='5; url=cpanel_user.php' /></div>";
} else {
echo "<div class='hidecontent'><h3 style='background-color:#FF0000; padding:5px;' align='center'>
<font color='#FFFF00'>Update Error</font></h3></div>";
}
}
Where is problem?
If it's updating all the rows then it probably means that you are not filtering the data which is to be updated. To filter the data, use the WHERE clause. Next ensure that all rows don't have a duplicate values else it would update all the rows as well. Consider the following code:
UPDATE `database` . `table` SET `Name` = 'Zahid Saeed' WHERE `Name` = 'Zahid';
The point is, the person named 'Zahid' is not alone in this world. You have to be more specific. If you can, always use the ID column to update a value as it is always unique just like so:
UPDATE `database` . `table` SET `Name` = 'Zahid Saeed' WHERE `id` = 'Some ID'
If you can't get the ID, use the AND clause to match multiple columns to filter the data more accurately like this:
UPDATE `database` . `table` SET `Name` = 'Zahid Saeed' WHERE `Name` = 'Zahid' AND `Age` = 19 AND `Email` = 'abc#hotmail.com'
Your query should be
"UPDATE inv
SET updatestartdate='$updatestartdate',
updateenddate='$updateenddate',
status='$status'
WHERE id = '".$id."'";
I'd like to check if a value is already in the table.
The structure of my table is this:
ApplicantId = INT
EventId = INT
StudentId = INT
No need to use unique because these table has dependencies.
Below is what I have tried so far:
include('../connectdb.php');
$ScholarPointId = $_GET["ScholarPointId"];
$Point = $_GET["Point"];
$ScholarId = $_GET["ScholarId"];
$EventId = $_GET["EventId"];
$verifysql = mysql_query("SELECT EventId FROM scholar_attended_events WHERE ScholarId ='$ScholarId' ");
#$resultVerify = mysql_fetch_assoc($verifysql);
$num_rows = mysql_num_rows($verifysql);
if( $num_rows > 0 )
{
$script = "<script>
alert('The user has already attended this event!');
</script>";
header('updatescholarpoints.php');
exit();
}
else{
$result = mysql_query("UPDATE scholar_points
SET scholar_points.Points =
scholar_points.Points + $Point
WHERE scholar_points.ScholarPointId = '$ScholarPointId' ") or die(mysql_error());
mysql_query("INSERT INTO scholar_attended_events (EventId , ScholarId) VALUES( '$EventId' , '$ScholarId' ) ")
or die(mysql_error());
}
?>
What I want is to check if the EventId is already in taken by the Student = StudentId. If so, then system will prompt an alert box. Otherwise, Update and Insert into respective table. How can I do this? It seems I miss something in here. If you could help, I really appreciate it.
just missing an = ?
$verifysql = mysql_query("SELECT EventId FROM scholar_attended_events WHERE ScholarId =$ScholarId ");
(and use PDO or mysqli, your code is really in a deprecated mode)
I'm trying to add a value to a row in a database table that currently has other values. I want to effect only one field in the table. here's what I'm doing:
$sql = "UPDATE users SET photo = ".$m_fname." WHERE pin = ".$_SESSION['pin'].";";
What's the correct way to do this?
Here's a little more:
$m_fname = mysql_real_escape_string($dest_filename);
$sql = "UPDATE users SET photo = ".$m_fname." WHERE pin = ".$_SESSION['pin'].";";
$res = #mysql_query($sql);
if (!$res) {
$errors[] = "Could not run query.";
break;
}
Before anyone else downvotes..
I'm aware I should be using mysqli. Sorry if I offended anyone by using an old function.
Maybe this helps, it's a different implementation and it includes a proper way of data-sanitation:
$dbSession = new PDO('mysql:host=***;dbname=***', '***', '***');
$updateQuery = $dbSession->prepare('
UPDATE
`users`
SET
`photo` = :photo
WHERE
`pin` = :pin');
$updateQuery->bindParam(':photo', $m_fname, PDO::PARAM_STR);
$updateQuery->bindParam(':pin', $_SESSION['pin'], PDO::PARAM_INT); // or 'PARAM_STR'
$updateQuery->execute();
See documentation for more functions available:
PHP Database Objects
Since you are setting the field photo to a string value, consider using single qutation
$sql = "
UPDATE `users`
SET `photo` = '" . $m_fname . "'
WHERE `pin` = " . $_SESSION['pin']; // Not clear what pin is (string or int)
What is the problem that you are facing? the code seems to be fine, provided that the PIN field is the primary unique key, and that photo is a varchar
use "'" for non number
$sql = "UPDATE users SET photo = '".$m_fname."' WHERE pin = '".$_SESSION['pin']."';";
$m_fname = mysql_real_escape_string($dest_filename);
$sql = "UPDATE users SET photo = '{$m_fname}' WHERE pin = '{$_SESSION['pin']}';";
$res = #mysql_query($sql);
if (!$res) {
$errors[] = "Could not run query.";
break;
}
I know this isn't so complicated but I can't remember how to do.
I just need to know the next auto increment.
$result = mysql_query("
SHOW TABLE STATUS LIKE Media
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];
...but i won't work for me, what am I doing wrong?
$result = mysql_query("
SHOW TABLE STATUS LIKE 'Media'
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];
The name of the table needed to be wrapped with single quotes like this: 'table_name'
So it works just fine now.
:)
The query should look like this:
SHOW TABLE STATUS WHERE `Name` = 'Media';
Another way, but slow, is:
SELECT AUTO_INCREMENT FROM information_schema.`TABLES` T where TABLE_SCHEMA = 'myScheme' and TABLE_NAME = 'Media';
The information_schema is mostly usefull for getting data from many schemes.
You can also use this function
function getNextValue(){
$query = "SHOW TABLE STATUS LIKE 'vendors'";
dbconnect();
$results=mysql_query($query);
if(mysql_errno() != 0) {
$result['count'] = -1;
$result['error'] = "Error: ".mysql_error();
} else {
$result['count'] = mysql_num_rows($results);
for($counter=0;$counter<$result['count'];$counter++) {
$result[$counter] = mysql_fetch_assoc($results);
}
}
return $result[0]['Auto_increment'];
mysql_close();
}
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "database_name"
AND TABLE_NAME = "table_name";
if you need to know the next auto_increment, then it's 99% likely you're doing it wrong. instead of the getting the next auto_increment, you should just do the insert you're about to do, then use SELECT LAST_INSERT_ID() to get the auto_increment value from that insert.
if you try to guess the next auto_increment value and you have multiple users doing it at the same time, you'll frequently get the wrong value.
I have to add some enum options to a database table. The problem being, I will have to do this to several tables & databases, and they may not all have the same enum data type. Is there a why to write an alter table query or something similar to append options to a enum data type?
If there is no way to do this purely in MySQL, how would you approach this problem with a PHP script?
there is not easy way to append enum values.
this is ugly and untested, but i think it will give you an idea:
<?php
$sql = "select table_schema
, table_name
, column_name
, column_type
, is_nullable
, column_default
from information_schema
where data_type = 'enum'";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
// these are important --> leading comma --v close paren --v
$new_enum = substr($row['column_type', 0, -1) . ",'new','enums','here')"
$sql = "alter table `{$row['table_schema']}`.`{$row['table_name']}`";
$sql .= " modify column `{$row['column_name']}`";
$sql .= " $new_enum";
$sql .= ($row['is_nullable'] == 'YES') ? " NULL" : " NOT NULL";
$sql .= " default $row['column_default']";
mysql_query($sql);
}
you could probably do this "purely in mysql" with a stored procedure, but that's more than i can wrap my brain around right now. :)