php mysql insert - php

I am pretty new in php programming and i'm facing with one trouble here. Probably it will be simple for all of you, but ok..
When i try to insert one row into mysql table named "novica" first time it works ok, but after that i'm unable to add any new row's. But when i delete this row, i can add one...but again, only one. I don't know what cause this.
here is my little php:
<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sola", $con);
mysql_query("INSERT INTO novica (naslov, vsebina, avtor, ustvarjeno) VALUES('$_POST[address]', '$_POST[content]', 'Klemen', NOW())");
mysql_close($con);
?>
And here is mysql export sql code:
-- phpMyAdmin SQL Dump
-- version 3.4.10.1deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 22, 2012 at 06:41 PM
-- Server version: 5.5.28
-- PHP Version: 5.3.10-1ubuntu3.4
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `sola`
--
-- --------------------------------------------------------
--
-- Table structure for table `novica`
--
CREATE TABLE IF NOT EXISTS `novica` (
`id_novica` int(10) NOT NULL,
`naslov` text COLLATE utf8mb4_bin NOT NULL,
`vsebina` text COLLATE utf8mb4_bin NOT NULL,
`avtor` text COLLATE utf8mb4_bin NOT NULL,
`ustvarjeno` date NOT NULL,
`posodobljeno` date DEFAULT NULL,
PRIMARY KEY (`id_novica`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
--
-- Dumping data for table `novica`
--
INSERT INTO `novica` (`id_novica`, `naslov`, `vsebina`, `avtor`, `ustvarjeno`, `posodobljeno`) VALUES
(0, 'a', 'a', 'Klemen', '2012-11-22', NULL);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;

Your primary key is not set to auto increment. As such when you insert a new row, the id of zero is possibly assigned, but any subsequent insert fails as that id is already taken.
Either add the id into the insert statement or make the id_novica auto increment - http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
CREATE TABLE IF NOT EXISTS `novica` (
`id_novica` int(10) NOT NULL AUTO_INCREMENT,
`naslov` text COLLATE utf8mb4_bin NOT NULL,
`vsebina` text COLLATE utf8mb4_bin NOT NULL,
`avtor` text COLLATE utf8mb4_bin NOT NULL,
`ustvarjeno` date NOT NULL,
`posodobljeno` date DEFAULT NULL,
PRIMARY KEY (`id_novica`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

It always god to check error (mysql_error()) to find out what the problem. If that is problematic, display your query, execute by hand in console and check what error you get.
In general your primary key id_novica have to be unique, hence failure of subsequent inserts. You should either change values or define your primari key as AUTO_INCREMENT, to tell mysql to take care of dealing with this. So instead of
`id_novica` int(10) NOT NULL,
you shall have
`id_novica` int(10) NOT NULL AUTO_INCREMENT,
and in your INSERTS do not provide id_novica at all.
You also should not be using mysql_ exitension - it is deprecated. Switch to mysqli_ but if you are starting, jump directly to PDO instead

add unique id to your insert or update your id column to be automatically incremented:
ALTER TABLE `novica` CHANGE `id_novica` `id_novica` INT( 10 ) NOT NULL AUTO_INCREMENT

Related

The phpMyAdmin configuration storage is not completely configured, some extended - Xammp - Version -7.3.8-0-VC15

I updated Xampp version - Latest version = 7.3.8-0-VC15
I am getting these errors -
Error 1-
The phpMyAdmin configuration storage is not completely configured, some extended features have been deactivated. Find out why.
I clicked on to find out why but did not understand that -
Error 2 -
mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'#'localhost' (using password: NO)
Error 3-
Connection for controluser as defined in your configuration failed.
I tried many solutions but not solved.
It's a mistake for missing some defaults table for default phpmydamin database. For solve this mistake you must to run following queries in phpmyadmin database
I suggest you download and install JetBrains DataGrip and connect this software with your local database and run following query on it to fixing phpmyadmin problem. another way is if you are friendly with command line log in to mysql with command line and run queries.
CREATE TABLE IF NOT EXISTS `pma_users` (
`username` varchar(64) NOT NULL,
`usergroup` varchar(64) NOT NULL,
PRIMARY KEY (`username`,`usergroup`)
)
COMMENT='Users and their assignments to user groups'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE IF NOT EXISTS `pma_usergroups` (
`usergroup` varchar(64) NOT NULL,
`tab` varchar(64) NOT NULL,
`allowed` enum('Y','N') NOT NULL DEFAULT 'N',
PRIMARY KEY (`usergroup`,`tab`,`allowed`)
)
COMMENT='User groups with configured menu items'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE IF NOT EXISTS `pma_navigationhiding` (
`username` varchar(64) NOT NULL,
`item_name` varchar(64) NOT NULL,
`item_type` varchar(64) NOT NULL,
`db_name` varchar(64) NOT NULL,
`table_name` varchar(64) NOT NULL,
PRIMARY KEY (`username`,`item_name`,`item_type`,`db_name`,`table_name`)
)
COMMENT='Hidden items of navigation tree'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
Then add following code to config.inc.php file:
$cfg['Servers'][$i]['users'] = 'pma_users';
$cfg['Servers'][$i]['usergroups'] = 'pma_usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma_navigationhiding';

Unicode characters only partially supported in PHP / MySQL?

I wrote a PHP script that creates 5 databases and inserts some data (using $mysqli->multi_query). When I tried it out, unicode (ä ö ü ß •) was broken, so I called utf8_decode on the whole sql query. However, this worked only for ä ö ü, not for ß • and not for uppercase Ä Ö Ü. This really confuses me.
Output of ß: �?Ÿ
I'm not a specialist in different character encodings, but until now, I always managed to get the proper output.
All my files are encoded in UTF-8 and the meta tag
<meta charset="utf-8">
is in every website.
Can someone help me on this?
Here are parts of the SQL query (it's really, really long)
-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Erstellungszeit: 22. Mrz 2017 um 21:29
-- Server-Version: 10.1.21-MariaDB
-- PHP-Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
CREATE TABLE `forum` (
`id` int(10) UNSIGNED NOT NULL,
`autor` varchar(50) NOT NULL,
`text` varchar(2000) NOT NULL,
`datum` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
. . .
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Do not ever use the PHP utf8_decode() function. The only thing it will do is irrecoverably mangle UTF-8 data, by (poorly) converting it to ISO8859-1. Remove it from anywhere it appears in your application.
Your table has DEFAULT CHARSET=latin1. Change that to utf8mb4 to store Unicode text:
ALTER TABLE `forum` CONVERT TO CHARACTER SET utf8mb4;
If and only if your MySQL install is too old to support utf8mb4, use utf8 instead. The utf8 character set supports a limited subset of Unicode (in particular, it does not support emoji), but will at least support a wider selection of characters than latin1.

How to backup SQL data base with PHP

How can I make a SQL file with PHP that contains SQL script to create exact copy of my entire database. Like I can do it in PhpMyAdmin.
For example:
I have a database with few tabel and I would like PHP script to to return SQL script like:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `first_db`
--
-- --------------------------------------------------------
--
-- Structure of table `my_table`
--
CREATE TABLE IF NOT EXISTS `my_table` (
`id` int(11) NOT NULL,
`naslov` text COLLATE utf8_slovenian_ci NOT NULL,
`besedilo` text COLLATE utf8_slovenian_ci NOT NULL,
`datum` text COLLATE utf8_slovenian_ci NOT NULL,
`avtor` text COLLATE utf8_slovenian_ci NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_slovenian_ci;
--
-- Fill data for `my_table`
--
INSERT INTO `my_table` (`id`, `naslov`, `besedilo`, `datum`, `avtor`) VALUES
just use exec() in php with the following shell command
exec('mysqldump –u [user name] –p[password] [database name] > [dump file]');
dont forget that exec can be disabled in php.ini if it doesn't work

Syntax error, unexpected TEXT_STRING (Wamp/MySQL Workbench)

My dad-in-law's webserver broke down yesterday, and we are trying to get a new one up running.
We transfered the frm/MYD/MYI files to my computer, and I have now managed to restore the sql files and tables. But when I tried to launch the page, it just showed up a load of gibberish.
I then opened the files in MySQL Workbench and found out that all (') had been replaced with (`)'s.
So after painstakingly replacing them, I tried to forward engineer again, but still the same gibberish was shown. I checked the code again and noticed that I'm getting this error, all the way through the code at 'x':
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS 'x' DEFAULT CHARACTER SET latin1 ;
USE 'x' ;
-- -----------------------------------------------------
-- Table 'x'.'configuration'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'x'.'configuration' (
'config_item' VARCHAR(64) NOT NULL DEFAULT '',
'config_value' VARCHAR(255) NOT NULL DEFAULT '')
ENGINE = MyISAM
DEFAULT CHARACTER SET = latin1;
Any idea what the heck I'm doing wrong?
#Sirko
Sooooo, like this?
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `x` DEFAULT CHARACTER SET latin1 ;
USE `x` ;
-- -----------------------------------------------------
-- Table `x`.`configuration`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `x`.`configuration` (
`config_item` VARCHAR(64) NOT NULL DEFAULT '',
`config_value` VARCHAR(255) NOT NULL DEFAULT '')
ENGINE = MyISAM
DEFAULT CHARACTER SET = latin1;
Try this:
CREATE TABLE IF NOT EXISTS `x`.`configuration` (
`config_item` VARCHAR(64) NOT NULL DEFAULT '',
`config_value` VARCHAR(255) NOT NULL DEFAULT '')
ENGINE = MyISAM
DEFAULT CHARACTER SET = latin1;

mysqli prepare statement using select failing for some unknown reason

Why is this failing? I am connected to the correct db, and keys is definitely there.
//verify key against key list in database
if ($prep_statement = $db->prepare("SELECT id FROM key_list WHERE keys=?"))
{
$prep_statement->bind_param('s',$key);
$prep_statement->execute();
echo $prep_statement->num_rows;
$prep_statement->close();
} else {
printf("Prepared Statement Error: %s\n", $db->error);
die();
}
I get the following error, and the if statement is returning false
Prepared Statement Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key=?' at line 1
Here's my table structure:
-- MySQL dump 10.11
--
-- Host: localhost Database: gameserverdev
-- ------------------------------------------------------
-- Server version 5.0.51a-24+lenny4
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `key_list`
--
DROP TABLE IF EXISTS `key_list`;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
CREATE TABLE `key_list` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`keys` char(16) NOT NULL,
`datetime_registered` datetime NOT NULL,
`banned` tinyint(1) NOT NULL,
`comment` char(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
SET character_set_client = #saved_cs_client;
--
-- Dumping data for table `key_list`
--
LOCK TABLES `key_list` WRITE;
/*!40000 ALTER TABLE `key_list` DISABLE KEYS */;
INSERT INTO `key_list` VALUES (2,'1111222233334444','2010-07-31 16:04:30',0,NULL);
/*!40000 ALTER TABLE `key_list` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Dumping routines for database 'gameserverdev'
--
DELIMITER ;;
DELIMITER ;
/*!40103 SET TIME_ZONE=#OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=#OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=#OLD_SQL_NOTES */;
-- Dump completed on 2010-07-31 23:00:56
You are binding a result, not binding a parameter. Change bind_result to bind_param and it should work correctly.
key is a MySQL reserved word. You'll need to either change the column name to something that is not a reserved word, or you'll need to quote it. In MySQL, the default identifier quote character is the backtick, `, though if ANSI Quotes are enabled, you can use the double quote, ", instead.
So, your statement should be:
SELECT id FROM key_list WHERE `key` = ?

Categories