简体   繁体   中英

Very strange situation with Can't connect to local MySQL server

I'm experiencing a strange situation, and it's the first time in 5 years...

my code:

<?php include("static/inc/settings.php"); ?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>News</title>
    <link rel="stylesheet" href="static/css/style.css" media="screen" />
    <script type="text/javascript" src="static/js/jquery.js"></script>
    <script type="text/javascript" src="static/js/tinymce.min.js"></script>
    <script type="text/javascript" src="static/js/script.js"></script>
</head>
<body>
    <div id="master">
        <?php if(isset($_POST["inserisci"])){
            $tipo=$_POST["tipo"];
            $titolo=$_POST["titolo"];
            $testo=$_POST["testo"];
            $link=$_POST["link"];
            $info=pathinfo($_FILES["immagine"]["name"]);
            $ext=$info["extension"];
            $newname=md5($titolo.time()).".".$ext;
            $target="static/uploads/".$newname;
            move_uploaded_file($_FILES['immagine']['tmp_name'],$target);
            mysql_query("INSERT INTO links(url) VALUES('".$link."');") or die(mysql_error()); ?>
            <div class="inserito scompari">La news &egrave; stata inserita</div>
        <?php } ?>
        <div id="header">
            <?php include("static/inc/menu.php"); ?>
        </div>
        <div id="main">
            <div class="block">
                <form enctype="multipart/form-data" id="inserisci_news" method="post">
                    <p>Tipo<br /><select name="tipo" id="tipo"><option value=""></option><option value="sinistra">sinistra</option><option value="sopra">sopra</option></select></p>
                    <p>Titolo<br /><input type="text" name="titolo" id="titolo" value="ciao" /></p>
                    <p>Testo<br /><textarea name="testo" id="testo">prova</textarea></p>
                    <p>Link<br /><input type="text" name="link" id="link" value="http://www.google.it" /></p>
                    <p>Immagine<br /><input type="file" name="immagine" value="" /></p>
                    <input type="submit" name="inserisci" value="Inserisci" />
                </form>
            </div>
        </div>
    </div>
</body>
</html>

basically, when I compile the form, I get an error in the query: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

but if I remove the if(isset($_POST["inserisci"])){ cycle.... It goes!!!

What the hell?

In settings.php there's the connection:

<?php $dbname="xxx";
$user="xxx";
$pswd="xxx";
$host="xxx";
if(!isset($link)){
$link=mysql_connect($host, $user, $pswd);
if(!$link){
    die('Could not connect: '.mysql_error());
}
mysql_set_charset('utf8', $link);
$db_selected=mysql_select_db($dbname, $link);
if(!$db_selected){
    die('Could not use db: '.mysql_error());
}
}
date_default_timezone_set('Europe/Rome'); ?>

I think the problem is that you're doing everything with global variables and you've stomped on yourself.

The relevant line in settings.php:

$link=mysql_connect($host, $user, $pswd);

The relevant line in your code:

$link=$_POST["link"];

What you have done is overwritten the variable containing the MySQL connection with something else. This is probably resulting in your query not having a valid connection object to use. I don't know why removing the isset() conditional appears to cause the error to disappear, but I would recommend changing the name of either of these variables and seeing what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM